2017-02-10 10 views
0

이것은 내가 작성한 것입니다. 나는 내 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; 
} 
+0

어디에서 프로그램에서 멈 춥니 까 ..... while 루프에도 들어갈 수 있습니까? 다른 print 서술문을 추가하고 그런 식으로 디버그하십시오. –

+0

'printf ("\ n % 1.0lf \ t \ t \ t % .1lf", count, area_uncut);''int' ('count') % lf', 이것은 정의되지 않은 행동입니다 ('% d'로 변경). –

+6

while (count <= years);'-'는 빈 루프 본문을 만듭니다. 컴파일러에서 전체 경고를 켜면 경고해야합니다. – Barmar

답변

4

이 줄 끝의 추가 ; 있습니다 : while (count <= years);

그것은 count 전혀 업데이트되지 않기 때문에 영원히 반복하는 원인이되는 while 루프 빈 몸으로 구문 분석됩니다. 여기

바보 같은 실수의 종류를 방지하는 방법입니다 :이 스타일로

while (count <= years) { 
    count = count + 1; 
    new_area = area_uncut + (rate * area_uncut); 
    printf("\n%d\t\t\t%.1f", count, area_uncut); 
    area_uncut += new_area; 
} 

, 여분의 다음 {가 줄의 끝에있는 커니 핸 및 리치 스타일을 사용하면 블록을 제어 시작 ;은 입력 할 확률이 훨씬 적어 부 자연스러운 것으로 더 쉽게 자리 매김 할 수 있습니다.

또한 countint으로 정의되어 있으므로 printf 형식도 잘못되었습니다. 확실히 더 많은 경고가 활성화되도록 컴파일하십시오.

관련 문제