这个程序代码是我复制书上的例题,可是运行时怎么结果和书上的结果不一样?
别人运行和书里例题复制下来的程序,结果和书上给出结果一样。怎么我运行的时候,结果却和书上的不一样?/* Program 4.6 The almost indefinite loop - computing an average */
#include <stdio.h>
#include <ctype.h> /* For tolower() function */
int main(void)
{
char answer = 'N'; /* Records yes or no to continue the loop */
double total = 0.0; /* Total of values entered */
double value = 0.0; /* Value entered*/
int count = 0; /* Number of values entered */
printf("\nThis program calculates the average of any number of values.");
for( ;; ) /* Indefinite loop */
{
printf ("\nEnter a value: "); /* Prompt for the next value */
scanf(" %lf", &value); /* Read the next value */
total += value; /* Add value to total */
++count; /* Increment count of values */
/* check for more input */
printf("Do you want to enter another value? (Y or N): ");
scanf(" %c", &answer ); /* Read response Y or N */
if( tolower(answer) == 'n' ) /* look for any sign of no */
break; /* Exit from the loop */
}
/* output the average to 2 decimal places */
printf ("\nThe average is %.2lf\n", total/count );
return 0;
}
① 2.5 ② 3.5 ③ 6 ,结果是4,我的结果是0.