I'm a beginner in programming and I've been trying to make a calculator console app in C for self entertainment, however there has been one minor problem that I thought I could solve easily, but it seems like I'm missing something. I made the system foolproof, as one could say, so you could not type letters instead of numbers and such (I'm bad at explaining, but you get the idea). The problem is that whenever I type the wrong sign and the wrong number B at the same time, both of the error messages appear, which shouldn't happen. I thought it's because of the goto function, which I read isn't very good to use, so I put a check whether an error message has already been shown (bad=1;), which works only with the first number for some reason. What am I doing wrong here?
while (1) {
double a, b, f=1;
char znak1;
int bad;
getagin:
bad = 0;
if(scanf("%lf", &a)) printf("");
else {
if(bad == 0) printf("Incorrect number, please write the problem again.\n---\n");
while(getchar() != '\n');
bad = 1;
goto getagin;
}
scanf(" %c", &znak1);
char result[32];
int n = f;
if (znak1 == '+') printf("");
else if (znak1 == '-') printf("");
else if (znak1 == '/') printf("");
else if (znak1 == '*') printf("");
else if (znak1 == '^') printf("");
else {
if(bad == 0) printf("Incorrect sign, please write the problem again.\n---\n");
bad = 1;
goto getagin;
}
if(scanf("%lf", &b)) printf("");
else {
if(bad == 0) printf("Incorrect number, please write the problem again.\n---\n");
while(getchar() != '\n');
bad = 1;
goto getagin;
}
if (znak1 == '+') snprintf(result, sizeof(result), "%.*f", n, a + b);
else if (znak1 == '-') snprintf(result, sizeof(result), "%.*f", n, a - b);
else if (znak1 == '/') snprintf(result, sizeof(result), "%.*f", n, a / b);
else if (znak1 == '*') snprintf(result, sizeof(result), "%.*f", n, a * b);
else if (znak1 == '^') snprintf(result, sizeof(result), "%.*f", n, pow(a, b));
printf("Solved problem: %.*f %c %.*f = %s\n---\n", n, a, znak1, n, b, result);
}