2010-09-15 162 views
3

嗨......它的新手再次:)我組建了一個程序,將計算無論是三角形或正方形的區域,然後提示用戶他們是否希望計算另一個。我已經得到了代碼工作,它將計算任何形狀的面積,但不會繼續執行其餘的代碼。例如,選擇方形,計算面積,然後返回到正方形的提示。我假設它又是一次循環,但不知道如何阻止循環繼續進行。While循環幫助

繼承人我的代碼:

#include<stdio.h> 
#include<math.h> 

int main(void) 

{ 
    float sq_side, tri_base, tri_height, Area; 
    char shape, cont = 'Y'; 

    printf("Please select the type of shape you desire to calculate the area for:\n"); 
    printf("                  \n"); 
    printf(" Square = S        Triangle = T    \n"); 
    printf(" -------         x      \n"); 
    printf(" :  :         x x      \n"); 
    printf(" :  :         x x     \n"); 
    printf(" -------         xxxxxxx     \n"); 
    printf("                  \n"); 
    printf("Please select either S or T:"); 
    scanf("%c", &shape); 
    while (cont != 'n' && cont != 'N') 

     if (shape == 'S' || shape == 's') 
     { 
      printf("What is the length of the sides of the square?:\n"); 
      scanf("%f", &sq_side); 

      Area = pow(sq_side,2); 

      printf("The area of the square is %.2f.\n", Area); 
     } 

     else if (shape == 'T' || shape == 't') 
     { 
      printf("What is the length of the base of the triangle?:\n"); 
      scanf("%f", &tri_base); 
      printf("What is the height of the triangle?:\n"); 
      scanf("%f", &tri_height); 

      Area = 0.5 * tri_base * tri_height; 

      printf("The area of the triangle is %.2f.\n", Area); 
     } 

     else 
     { 
     printf("Error: You have select an incorrect option."); 
     } 

     printf("Do you wish to calculate a new shape?"); 
     fflush(stdin); 
     scanf("%c", &cont); 

    return(0); 

} 
+4

總是檢查'scanf()'的返回值!此外,您不記錄'N'或'n'迴應。當心'%c'轉換代碼;它不會像大多數其他'scanf()'轉換代碼那樣跳過空格,例如換行符。一般來說,避免'scanf()';改用'fgets()'和'sscanf()'。 – 2010-09-15 02:39:45

回答

8

你錯過了大括號。結果是隻有if語句(其中包含其他ifs的鏈)實際上在循環體中。 printf(及更高版本)不是此複合聲明的一部分。

while (cont != 'n' && cont != 'N') 
{ 
    if (shape == 'S' || shape == 's') 
    { 
     printf("What is the length of the sides of the square?:\n"); 
     scanf("%f", &sq_side); 

     Area = pow(sq_side,2); 

     printf("The area of the square is %.2f.\n", Area); 
    } 

    else if (shape == 'T' || shape == 't') 
    { 
     printf("What is the length of the base of the triangle?:\n"); 
     scanf("%f", &tri_base); 
     printf("What is the height of the triangle?:\n"); 
     scanf("%f", &tri_height); 

     Area = 0.5 * tri_base * tri_height; 

     printf("The area of the triangle is %.2f.\n", Area); 
    } 

    else 
    { 
    printf("Error: You have select an incorrect option."); 
    } 

    printf("Do you wish to calculate a new shape?"); 
    fflush(stdin); 
    scanf("%c", &cont); 
} 
3

Theres no loopets while while循環。所以,如果elseif else塊之外的代碼沒有被調用。

目前你的代碼轉換爲

while (cont != 'n' && cont != 'N') 
{ 
    if (shape == 'S' || shape == 's') 
    {} 
    else if (shape == 'T' || shape == 't') 
    {} 
    else 
    {} 
} 

printf("Do you wish to calculate a new shape?"); 
fflush(stdin); 
scanf("%c", &cont); 

當你想

while (cont != 'n' && cont != 'N') 
{ 
    if (shape == 'S' || shape == 's') 
    {} 
    else if (shape == 'T' || shape == 't') 
    {} 
    else 
    {} 

    printf("Do you wish to calculate a new shape?"); 
    fflush(stdin); 
    scanf("%c", &cont); 
} 

記住,如果你不包括在控制結構大括號,它只調用的下一條語句,這在你的情況是一系列嵌套的if-else if語句。

希望它有幫助 - Val