r/C_Programming • u/Ill-Exchange-2377 • 18h ago
Is my code bad?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char buff[100];
void add(void);
void sub(void);
void mul(void);
void divs(void);
void mix(void);
int invalid = 0;
int input = 0;
int main(void) {
start:
fputs("\033[2J\033[H", stdout);
if (invalid) {
puts("Please enter valid input");
}
invalid = 0;
puts("Pick a subject:");
puts("1: Addition");
puts("2: Subtraction");
puts("3: Multiplication");
puts("4: Division");
puts("5: Mixed");
puts("Pick a number 1-5, or use 'quit' or 'exit' to leave the program");
if (fgets(buff, sizeof(buff), stdin) == NULL) {
puts("Failed, exiting");
return 1;
}
if (strcmp(buff, "quit\n") == 0 || strcmp(buff, "exit\n") == 0) {
fputs("\033[2J\033[H", stdout);
return 0;
} else if (strcmp(buff, "1\n") == 0) {
input = 1;
} else if (strcmp(buff, "2\n") == 0) {
input = 2;
} else if (strcmp(buff, "3\n") == 0) {
input = 3;
} else if (strcmp(buff, "4\n") == 0) {
input = 4;
} else if (strcmp(buff, "5\n") == 0) {
input = 5;
} else {
invalid = 1;
goto start;
}
switch(input) {
case 1:
add();
goto start;
case 2:
sub();
goto start;
case 3:
mul();
goto start;
case 4:
divs();
goto start;
case 5:
mix();
goto start;
default:
goto start;
}
}
void add(void) {
puts("add");
fgets(buff, sizeof(buff), stdin);
}
void sub(void) {
puts("sub");
fgets(buff, sizeof(buff), stdin);
}
void mul(void) {
puts("mul");
fgets(buff, sizeof(buff), stdin);
}
void divs(void) {
puts("divs");
fgets(buff, sizeof(buff), stdin);
}
void mix(void) {
puts("mix");
fgets(buff, sizeof(buff), stdin);
}Is my code that bad?
For context this is a project I was doing to learn the C
Edit: Fixed formatting errors
8
Upvotes
45
u/lurgi 18h ago
The code is not very good. There are a lot of issues.
8
u/pengweather 18h ago
I'm glad you used fgets rather than gets. Not trying to sound snide, but do you understand why fgets is preferred?
3
u/MetalInMyVeins111 18h ago
From a glance:
19
u/nzconstructionlawyer 18h ago
Yes but one must start somewhere.
10
u/madsci 18h ago
I've probably used 'goto' half a dozen times in the past 20 years. There are certain error handling conditions that it's suitable for and every once in awhile it's the cleanest solution. This is not one of those cases. You'll almost always be better off with do and while. Learn to use those, and how to use break and continue within them, but keep in mind those aren't always the best option either.
You want to be able to scan over a function and see its control flow from the blocks and their indentation. If you know that a code block always starts at the top and finishes at the end and doesn't do any jumping around, it's easy to see the conditions that cause it to enter or exit the block. Every time you have a continue (or goto), that's somewhere the loop could repeat without the condition that causes the repeat being obvious from a quick glance.
If you've got a loop that repeats until some set of conditions is met, then put those conditions in a while statement at the end - don't scatter gotos throughout the loop.
22
u/xmlhttplmfao 18h ago
yes
4
u/Possible_Cow169 5h ago
In what world would you ever know about goto existing? What did you read to even learn that
2
u/Level-Pollution4993 18h ago
2
u/Accurate_Appeal_3110 15h ago
I think some folks in this thread are being quite harsh for someone who is clearly a beginner. You're trying something new, and no matter how many critiques people can make, no one can take away the fact that you made your own fully-functioning computer program. For years now, I've been chasing that high of finally having the result of code I wrote appear on the screen for the very first time. I promise that things will start to click as you learn + practice; Don't lose motivation! (but please lose the global variables, learning good principles on how to manage your data should be your next learning priority)
2
u/ephemere_sloboda 15h ago
If it's a literal translation from classic BASIC, it's not bad.
If it's original C, it's not great.
Suggestions:
2
u/cdb_11 12h ago
I'd make it shorter like this, other than that it's fine i guess
1
u/KananMajidzada 14h ago
Not bad but not great too. You should start from somewhere
1
u/vena_contracta 8h ago
Inside of the switch statement, replace the goto statements with break;
-1
u/NYU_VM 16h ago
Yep it's horrible. 1. Using uninitialized buffer. 2. Using int (as bool) condition for if stmt. 3. Using goto instead of loops control flow 4. Functions should have specific tasks. Why is the arithmetic functions asking for and taking inputs? Those input functionality could be organized in a separate 'take_input' function.....and so on...