2016-01-24 108 views
-5
#include <stdio.h> 

int main() { 

    char choice; 
    float x,y; 

    start: 
    printf("[c] Converts Celsius -> Fahrenheit\n[f] Converts Fahrenheit -> Celsius\n\n\n"); 
    printf("Enter Choice: "); 
    scanf("%c",&choice); 
    if (choice!='c' || choice!='f' || choice!='x') { 
      printf("Wrong Choice: Try Again!"); 
      goto start; 

     } if (choice!=x) 
      printf("Input Value: "); 
      scanf("%f",&x); 
       if (choice =='c') 
        y = 1.8 * x + 32 
       else 
        y = (x-32) * (5/9) 
      printf("Result: %.2f",y); 
      exit: 

    return 0; 
} 

我的導師發佈了這個,但是當我嘗試它時,它有錯誤,需要幫助解決它。使用goto語句for循環

+8

什麼錯誤?你嘗試了什麼?你能指望什麼? –

+3

在某些情況下使用'goto'被認爲是可以的。使用它而不是*循環*不是這些情況之一。你爲什麼不使用循環? –

+0

一個明顯的問題是檢查'選擇'的'if'語句。條件顯然總是如此:它不可能同時與'c','f'和'x'相等,並且測試是,如果它不等於它們中的任何一個,那麼做一些事情。如果這真的是你的意圖,你可以用'if(1)'代替它。 –

回答

2

有相當一些事情出錯,這代碼:

  1. 使用goto代替while循環不是使這個方式。
  2. 您的第二個ifif (choice != x))缺少大括號。這樣只有第一條語句被執行,如果是的話。其餘的是總是執行
  3. 您在計算後缺少分號;
  4. 您的布爾邏輯在第一個if不正確。
  5. 在你的第二個if你是比較一個變量,而不是一個固定值。
  6. 您的華氏攝氏溫度算法不正確。

希望這些提示可以幫助您。我不會發布正確的代碼,這取決於你還在學習;-)。所有的

0

首先,你應該忘記,還有就是goto聲明C.

所有程序包含一個goto聲明,我看到包括一些IBM的源代碼必須與goto陳述許多錯誤。

goto聲明使程序難以閱讀,更難以維護和修改它們。

使用goto聲明是一樣的交通violatins。:)

代替這段代碼

start: 
printf("[c] Converts Celsius -> Fahrenheit\n[f] Converts Fahrenheit -> Celsius\n\n\n"); 
printf("Enter Choice: "); 
scanf("%c",&choice); 
if (choice!='c' || choice!='f' || choice!='x') { 
     printf("Wrong Choice: Try Again!"); 
     goto start; 

    } 

,至少有一個無效的條件if語句

if (choice!='c' || choice!='f' || choice!='x') 

代替有效

if (choice!='c' && choice!='f' && choice!='x') 

你可以寫例如

enum { Celsius = 'c', Fahrenheit = 'f', Exit = 'x' }; 
char choice; 
int valid_input; 

do 
{ 
    printf("[c] Converts Celsius -> Fahrenheit\n" 
      "[f] Converts Fahrenheit -> Celsius\n" 
      "[x] Exit\n\n"); 

    printf("Enter Choice: "); 

    if (scanf("%c ", &choice) != 1) choice = Exit; 

    valid_input = choice == Celsius || choice == Fahrenheit || choice == Exit; 

    if (!valid_input) puts("Wrong Choice: Try Again!\n"); 
} while (!valid_input); 

程序中有許多錯誤。的Insetad列出他們,我將展示如何 程序可以寫

#include <stdio.h> 

int main(void) 
{  
    enum { Celsius = 'c', Fahrenheit = 'f', Exit = 'x' }; 
    char choice; 
    int valid_input; 

    do 
    { 
     printf("[c] Converts Celsius -> Fahrenheit\n" 
       "[f] Converts Fahrenheit -> Celsius\n" 
       "[x] Exit\n\n"); 

     printf("Enter Choice: "); 

     if (scanf("%c ", &choice) != 1) choice = Exit; 

     valid_input = choice == Celsius || choice == Fahrenheit || choice == Exit; 

     if (!valid_input) puts("Wrong Choice: Try Again!\n"); 
    } while (!valid_input); 


    switch(choice) 
    { 
     float x, y; 

     case Celsius: case Fahrenheit: 
      printf("Input Value: "); 
      if (scanf("%f", &x) == 1) 
      {     
       if (choice == Celsius) 
        y = 1.8 * x + 32; 
       else 
        y = (x - 32) * (5.0f/9.0f); 
       printf("Result: %.2f\n", y); 
      }     
     case Exit: 
      puts("Bye!"); 
      break; 
    } 

    return 0; 
}    

如果按順序進入

c 20 x 

那麼輸出將看起來像

[c] Converts Celsius -> Fahrenheit 
[f] Converts Fahrenheit -> Celsius 
[x] Exit 

Enter Choice: c 
Input Value: 20 
Result: 68.00 
Bye! 
+0

夥計們,我把一個錯誤的標題。 它應該是溫度轉換器使用GOTO語句 不在循環中。抱歉。 –