| Post Reply | RetroFed Forums Forum Index » C Programming Language |
| Author | Message | ||
|---|---|---|---|
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
Good afternoon! How are you all holding up today? I "accidentally" ate piece of cake at the cafe I'm currectly sitting in, even though I had promised myself not to eat sugar until next week. Fail. 😅 Don't mind the English language, since this is an excerpt from a larger piece of code. If I enter anything other than 'Y' and 'N' using a single character, the code works as I want it to, but, naturally, if I enter anything other than 'Y' and 'N' multiple times, the Feel free to NOT provide the correct answer right away, but instead, give me the topic or the function to read up on. 😊
|
||
| 8 Original | |||
kiri
ani.social
kiri@ani.socialJoined: 2024-06-08 |
read #1.5 and #7: K&R or just find examples of |
||
| 3 | |||
|
lemmysmash
piefed.social
lemmysmash@piefed.social Joined: 2026-02-26 |
I'm too lazy to confirm, but I think scanf (as well as many other I/O functions) works on top of the buffered input. I.e. when you scanned only one character on the first run of scanf but there are more left in the buffer, then subsequent calls will read from that buffer, e.g.
In general you have two options:
For both cases, do your research on how to do it :) That K&R from above should be a great start indeed. |
||
| 6 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
read #1.5 and #7: K&R or just find examples of getchar() or fgets()
Thanks! I'll look 'em up! :) |
||
| 2 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
I'm too lazy to confirm, but I think scanf (as well as many other I/O functions) works on top of the buffered input. I.e. when you scanned only one character on the first run of scanf but there are more left in the buffer, then subsequent calls will read from that buffer, e.g. printf("your prompt>") /* you type 123 */ /* buffer contains: "123" */ scanf(...) /* you scanned 1 char ("1") */ /* ...
Thanks very much! I had no idea there is such a thing as "unbuffered I/O". I'll look into it. :) |
||
| 3 | |||
|
hkwln
lemmy.ml
hkwln@lemmy.ml Joined: 2025-02-16 |
you could also use getchar() to only take the first char out of the buffer, if you didn't know already ;) |
||
| 3 | |||
|
ExperimentalGuy
programming.dev
ExperimentalGuy@programming.dev Joined: 2023-07-25 |
Use gets on a stack buffer, but don't enable stack canaries and make the stack both writable and executable. Your users will thank you. |
||
| 1 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
you could also use getchar() to only take the first char out of the buffer, if you didn't know already ;)
I'll try this soon. 😊 To my defense, I don't have a bathtub in which I could arrive at these kinds of ideas 🤣 |
||
| 2 | |||
iusemybrain
sh.itjust.works
iusemybrain@sh.itjust.worksJoined: 2026-06-06 |
I would do the loop instead the if condition, where letter != 'Y' or 'N' generally speaking, using while(1) for x86_64 programming is bad practice, specific cases like embedded system design is where it's more commonly used. |
||
| 1 | |||
| Post Reply |
Back to top |