Undefined loop not working in C

I am currently reading "Beginning C" by Ivor Horton. In any case, my undefined for twice prints my printf statement before moving on. I am sure that I am doing something wrong, but I copied the code directly from the book. I use Dev-C ++ if that matters. Here is the code ... Thanks

 #include <stdio.h> #include <ctype.h> // For tolower() function // int main(void) { char answer = 'N'; double total = 0.0; // Total of values entered // double value = 0.0; // Value entered // int count = 0; printf("This program calculates the average of" " any number of values."); for( ;; ) { printf("\nEnter a value: "); scanf("%lf", &value); total+=value; ++count; printf("Do you want to enter another value? (Y or N): "); scanf("%c", &answer); if(tolower(answer) == 'n') break; } printf("The average is %.2lf.", total/count); return 0; } 
+4
source share
3 answers

If we briefly review your program, here's what happens:

  • It prompts the user to enter a number.
  • The user enters a number and presses the enter key.
  • scanf reads the number, but leaves the queue line in the queue.
  • It prompts the user to enter Y or N.
  • It tries to read the character, but does not skip any spaces / newlines, so it ultimately consumes the newline remaining in the queue.

Obviously, we need to skip a new line. Fortunately, this is pretty simple, if not obvious: add a space at the beginning of the format string, for example:

 scanf(" %c", &answer); 

The space in the format string means "skip as many spaces as possible before reading the next thing." This is done automatically for most conversions, but not for strings or characters.

+6
source

Change this line

 scanf("%c", &answer); 

to

 scanf(" %c", &answer); 

A space will cause scanf to ignore spaces that precede the character you enter.

The space is the result of pressing Enter after providing the number.

+2
source

The code is OK, only the missing value is fflush(stdin) ; to scanf function. It can always be used before the scanf function to avoid these errors. The act of pressing the 'Enter' key gives a new line character \\ n \ as an input to the stdin buffer. Therefore, the first scanf function in a loop takes it as an input and does not wait for the user to enter values.

 #include <stdio.h> #include <ctype.h> // For tolower() function // int main(void) { char answer = 'N'; double total = 0.0; // Total of values entered // double value = 0.0; // Value entered // int count = 0; printf("This program calculates the average of" " any number of values."); while(1) { printf("\nEnter a value: "); fflush(stdin); scanf("%lf", &value); total+=value; ++count; printf("Do you want to enter another value? (Y or N): "); fflush(stdin); scanf("%c", &answer); if(tolower(answer) == 'n') break; } printf("The average is %.2lf.", total/count); getch(); return 0; } 

Also add the getch() function to view the results if you use the console.

-1
source

Source: https://habr.com/ru/post/1488615/


All Articles