【发布时间】:2017-02-10 00:09:30
【问题描述】:
这是我写的。我猜这可能与我的while 循环的逻辑有关,但我无法完全发现它!任何帮助表示赞赏!谢谢。
#include <stdio.h>
#include <math.h>
//Open main function.
int main(void)
{
double new_area, area_total = 14000, area_uncut = 2500, rate = 0.02, years;
int count = 0;
printf("This program is written for a plot of land totaling 14000 acres, "
"with 2500 acres of uncut forest\nand a reforestation rate "
"of 0.02. Given a time period (years) this program will output a table\n"
"displaying the number acres reforested at the end of "
"each year.\n\n\n");
printf("Please enter a value of 'years' to be used for the table.\n"
"Values presented will represent the number acres reforested at the end of "
"each year:>> ");
scanf("%lf", &years);
years = ceil(years);
printf("\n\nNumber of Years\t\tReforested Area");
while (count <= years);
{
count = count + 1;
new_area = area_uncut + (rate * area_uncut);
printf("\n%1.0lf\t\t\t%.1lf", count, area_uncut);
area_uncut += new_area;
}
return 0;
}
【问题讨论】:
-
while (count <= years);-- 那里的;创建了一个空循环体。在您的编译器中打开完整警告,它应该对此发出警告。 -
@KeineLust 这就是为什么它是一个警告,而不是一个错误。这是一个常见的错字,编译器会发出警告,以防它不是您真正的意思。
-
你启用什么标志来接收这样的警告?
-
@Barmar:我用
g++和-Wall编译,但它没有出现。我仍然赞成您的评论,因为删除该分号可以解决 OPs 问题。当我尝试使用gcc编译时,由于ceil而出现错误。 -
gcc 在 Linux 上忽略了这个标志 :(
标签: c loops while-loop