#include <stdio.h>
int main(){
while(1 == 1){
int a;
int b;
scanf("%d", &a);
scanf("%d", &b);
if(a > b){
printf("A is bigger than B\n");
}
else if(a == b){
printf("A and B are equal\n");
}
else{
printf("B is bigger than A\n");
}
}
}
Everything worked as expected but i ran the program again and it broke, im not sure why but it kept printing the last message over and over again
CodePudding user response:
That's because you are inputting floating-point numbers but your scanf
only accepts integers.
Change
int a; // -2, -1, 0, 1, 2, 3, 4, ...
int b;
scanf("%d", &a);
scanf("%d", &b);
To
float a; // -1.0, -0.5, 0.0, -0.5, 1.0, 2.0, ...
float b;
scanf("%f", &a);
scanf("%f", &b);
CodePudding user response:
The function scanf() returns the number of elements it has processed, you should check it.
if (scanf("%d", &a) != 1)
{
fprintf(stderr, "Error: Invalid input.”);
exit(EXIT_FAILURE);
}
You provided the format specifier %d to scanf, which expects an integer argument. So you can't use floating point numbers.
For more information about scanf, type man scanf on your linux terminal. Or you can visit the cpp.reference site.
I'd refrain from commenting on the condition for the while loop, as the comments already do.
CodePudding user response:
Now every time i run it the second time it gives me an error even tho i didnt input anything