I wrote a simple program with a function that calculates the area of a circle.
The program also asks to the user if he wants to calculate it again and if the input is 'N'
, the program is supposed to stop.
Here's the narrowed down test case:
#include<stdio.h>
#include<string.h>
int main(void)
{
float r;
char f;
do {
printf("Type the radius\n");
scanf("%f", &r);
printf("Repeat? [Press N for stop]");
scanf("%c", &f);
} while(f != 'N');
getch();
return 0;
}
but the loop never stops as it was intended to.
Do you have any suggestion?
scanf("%c", &f);
leaves a newline character in the input stream which is consumed in the next iteration. Add a space in the format string to tell scanf() to ignore whitespaces.
scanf(" %c", &f); // Notice the space in the format string.
replace
scanf("%c", &f);
with
f=getch();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With