r/C_Programming • • 11d 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

23 Upvotes

50 comments sorted by

View all comments

Show parent comment

-12

u/Ill-Exchange-2377 11d ago

I already did that, convert to an integer. It's unnecessary. I am not checking millions of numbers, I don't need to do all that. It's an unnecessary conversion, which I already tried. That's what the switch case used to do, input used to hold the return value of a function that converted a string to integer. I just quickly swapped it out for this string comparison that ended up working better anyway, because I didn't have to make sure I was passing a non integer to the conversion function, and it was overall simpler. I will have to convert at some point in this program anyway, I would prefer to only do so if I have to.
Secondly the functions are placeholders, I don't intend them to stay like that.
Thirdly, I don't get the issue with goto, why is everyone so triggered by it.

26

u/Slow-Race9106 11d ago

Don’t ask for advice if you can’t take a bit of constructive criticism.

8

u/ItsSkyWasTaken 11d ago edited 11d ago

I mean tbf...

  1. They're right; while their solution is suboptimal, converting the response to an integer is also suboptimal and adds unnecessary conversion overhead. Instead for something like this you should just put a switch directly on buff[0], since all valid inputs are single digits. If you want better error checking you can check if buff[1] == '\n' to verify the input is valid.
  2. The functions being placeholders does provide important context as to why they're not inlined.
  3. The goto criticism was definitely valid but at the same time they're entitled to ask as a beginner why using goto is bad. The answer we could've given them is that it tends to create spaghetti code, and C offers much more readable constructs like dedicated loop syntax (for and while) and branch syntax (if, combined with break and continue). The curly braces are much easier to track than arbitrary goto labels.

I don't exactly understand why their response got so much hate. The response had some valid points, and it seems unfair to just dismiss them as "you can't take criticism" just because they're the student.

3

u/ReallyEvilRob 11d ago

Because goto produces spaghetti code. There are lots of construct that make goto unnecessary since goto is the enemy of structured programming. Any beginner class in programming will teach you that. There are very specific cases where goto makes sense and you do not need to think about that until you are a more seasoned programmer.

8

u/dydhaw 11d ago

Mate you're either trolling hard or need a change in attitude because your code isn't just bad, it barely meets the criteria to be considered code in the first place. The parent comment gave you some concrete ways to improve, I'd shut up and take notes if I were you, instead of getting all defensive about this. That is only if you really care about learning and improving, of course.

6

u/ItsSkyWasTaken 11d ago edited 10d ago

The only fully correct thing in the parent comment was the goto comment. However, as the student, they still 100% have the right to ask why using goto is discouraged, so they understand why we don't use it and they can be a better programmer. Telling a student to "shut up and go with it" is not helpful. Instead, we as the teachers should be able to explain that "goto is bad because it tends to produce spaghetti code; instead, C provides much more readable syntax for loops and decisions (if, while, for, etc.), and curly braces are much easier to follow than arbitrary goto labels."

The parent comment was correct in pointing out that the string comparisons were bad, but the alternative solution they gave was not that much better. You do not need to convert the string to an integer; as the student pointed validly pointed out (and said they've already tried that), it adds a bunch of extra overhead that's not worth it when only dealing with single digits. Instead, the best way to parse that is to check buff[1] == '\n' (verify that the input is indeed one digit) and then put a switch directly on buff[0].

Additionally, their clarification on the functions being placeholders does indeed provide important context as to why their functions are not inlined. That is a valid comment to make in response to a comment that suggests their functions are bad because they're basically the same.

So yeah, they had valid points, and I don't understand the hate on that response. I don't think it's fair to simply dismiss their response as "you can't take criticism" just because they're the student.

6

u/dydhaw 11d ago

Honestly, you're right, I was being an asshole

2

u/Certain-Flow-0 11d ago

Input conversion is important because it separates input processing from your calculation logic