2016-01-24 182 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语句 不在循环中。抱歉。 –