Home > Back-end >  Can not correctly perform the scanf () function, how to solve
Can not correctly perform the scanf () function, how to solve

Time:10-12

I ask, where is the following code is wrong, can not get the correct results, performance not let enter Y or N, and direct access to line 13, the main problem in 18 to 22, the debug level no didn't find any problem, but is unable to correctly perform this line, the difficult to determine the correct input letters and downward "N" of the true and false, how to solve?


 # include & lt; stdio.h> 
# include & lt; Ctype. H>
# include & lt; Conio. H>
Int main (void)
{
Char answer='N';
Double total=0.0;
A double value=https://bbs.csdn.net/topics/0.0;
int count=0;
Printf (" \ nthis program calculates the average of any number of values. ");
For (; ; )
{
Printf (" \ n the Enter a value: ");
Lf the scanf (" % ", & amp; Value);
Total +=value;
+ + count;

Printf (" \ n Do you want to enter another value? (Y/N) : ");
The scanf (" % c ", & amp; Answer);

If (tolower (answer)=='n')
break;
}
Printf (" \ nthe business is %. 0 lf \ n ", total/count);

_getch ();
return 0;
}

CodePudding user response:

 for (; ; ) 
{
Printf (" \ n the Enter a value: ");
Lf the scanf (" % ", & amp; Value);
Total +=value;
+ + count;

Printf (" \ n Do you want to enter another value? (Y/N) : ");
getchar();
The scanf (" % c ", & amp; Answer);

If (tolower (answer)=='n')
break;
}


Add a getchar (), probably simply because there is first the scanf newline, just in a second the scanf, use first getchar () absorbed the newline character, if that's ok

CodePudding user response:

 the scanf (" % c ", & amp; Answer); 

This statement before adding a getchar ();
Because of this statement is % c, '\ n' match the input buffer, this' \ n 'is an input on the left,
  • Related