| Post Reply | RetroFed Forum Forum Index » C Programming Language |
| Author | Message | ||
|---|---|---|---|
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
Hello there! I am playing around at random for now before continuing reading the litterature that I am using to learn C. What is a less "ugly" way to break out the
|
||
| 9 Original | |||
redlemace
lemmy.world
redlemace@lemmy.worldJoined: 2025-03-03 |
an " if ... else ... " always needs an else to be an "if ... else" statement ;) but you can just create an "if..... " statement So you can change
into
|
||
| 6 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
an " if ... else ... " always needs an else to be an "if ... else" statement ;) but you can just create an "if..... " statement So you can change } if (choice == 'E') break; else; } printf("\nGoodbye!\n"); return 0; } into } if (choice == 'E') break; } printf("\nGoodbye!\n"); return 0; }
Ah, gotcha! Thanks! |
||
| 4 | |||
|
CameronDev
programming.dev
CameronDev@programming.dev Joined: 2023-06-18 |
The else is definitely not needed, you can delete it. As for the best way to climb out of a nested loop, this is one of the occasions that a goto is reasonable. The jump would be relatively short and localised, which is exactly what GOTO should be used for:
What's with the getchars slapped everywhere? I think its an attempt to flush stdin, but I think you need to do it repeatedly until you hit a newline or EOF. I think your next step is to refactor the code into some functions, you have two nearly identical code sections for adding a new pin, you can make that a function and simplify it. |
||
| 2 | |||
|
AlSweigart
programming.dev
AlSweigart@programming.dev Joined: 2026-05-07 |
The else isn't needed, as others have said. I'd find, install, and configure a linter tool, which can give you pointers on small stylistic issues like this. |
||
| 9 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
The else isn't needed, as others have said. I'd find, install, and configure a linter tool, which can give you pointers on small stylistic issues like this.
Sweet! I'll check it out! 😊 |
||
| 2 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
The else is definitely not needed, you can delete it. As for the best way to climb out of a nested loop, this is one of the occasions that a goto is reasonable. The jump would be relatively short and localised, which is exactly what GOTO should be used for: What's with the getchars slapped everywhere? I think its an attempt to flush stdin, but I think you need to do it repeatedly until you hit ...
"Slapped" 🤣 I think I forgot to remove the getchars after having used scanf, I had learned here. As you said, to clean up the input buffer. I had JUST written down in my TODO list to make a function of it! 😁 Thank you so much! 😊 |
||
| 2 | |||
|
sobchak
programming.dev
sobchak@programming.dev Joined: 2025-06-28 |
Yeah. Not necessarily applicable here, but in general, it's good practice to use guard clauses/early return when appropriate which don't have |
||
| 3 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
The else is definitely not needed, you can delete it. As for the best way to climb out of a nested loop, this is one of the occasions that a goto is reasonable. The jump would be relatively short and localised, which is exactly what GOTO should be used for: What's with the getchars slapped everywhere? I think its an attempt to flush stdin, but I think you need to do it repeatedly until you hit ...
I've been at it for two days without researching and I can't figure out how to "hand over" the updates of the history array from within my "new pin" function to the main() function. I have to learn about scopes. Good fun! |
||
| 2 | |||
|
CameronDev
programming.dev
CameronDev@programming.dev Joined: 2023-06-18 |
I've been at it for two days without researching and I can't figure out how to "hand over" the updates of the history array from within my "new pin" function to the main() function. I have to learn about scopes. Good fun!
Pass the history array into your new function via a pointer. I don't know what you mean by "without researching", but that is a very hardcore way to learn C. Respect! |
||
| 2 | |||
printf("%s", name);
piefed.blahaj.zone
akunohana@piefed.blahaj.zoneJoined: 2026-03-30 |
Pass the history array into your new function via a pointer. I don't know what you mean by "without researching", but that is a very hardcore way to learn C. Respect!
Thanks for the pointers. 😝 Oh, I just meant that I'm experimenting, evaluating and thinking instead of "just" searching for a straight answer on the world wide web. Edit: Also meaning, that when I post a question here, I have already writhen (spelling?) in pain for a few hours or a day without getting anywhere. |
||
| 2 | |||
|
CameronDev
programming.dev
CameronDev@programming.dev Joined: 2023-06-18 |
Thanks for the pointers. 😝 Oh, I just meant that I'm experimenting, evaluating and thinking instead of "just" searching for a straight answer on the world wide web. Edit: Also meaning, that when I post a question here, I have already writhen (spelling?) in pain for a few hours or a day without getting anywhere.
That's fair enough, and a good way to learn. Still, in this day and age with Claude etc, I respect the effort you're putting in. In the interests of proposing alternatives, you could also make the pin list a global variable. I'll let you work out why its a bad idea though :) |
||
| 2 | |||
| Post Reply |
Back to top |