| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:这个程序代码是我复制书上的例题,可是运行时怎么结果和书上的结果不一样?
只看楼主 加入收藏
churichenxi
Rank: 1
等 级:等待验证会员
帖 子:9
专家分:0
注 册:2012-6-16
结帖率:100%
收藏
 问题点数:0 回复次数:1 
这个程序代码是我复制书上的例题,可是运行时怎么结果和书上的结果不一样?
别人运行和书里例题复制下来的程序,结果和书上给出结果一样。怎么我运行的时候,结果却和书上的不一样?

/* 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.
搜索更多相关主题的帖子: computing function continue computing function continue 
2012-06-23 16:36
churichenxi
Rank: 1
等 级:等待验证会员
帖 子:9
专家分:0
注 册:2012-6-16
收藏
得分:0 
看来还是编译器的问题,我换了一个编译器运行结果就对了。
2012-06-25 07:24
快速回复:这个程序代码是我复制书上的例题,可是运行时怎么结果和书上的结果不一 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015746 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved