华氏度到Celsius的C ++转换。但为什么它不起作用?

时间:2017-10-26 17:36:30

标签: c++

华氏度到摄氏度或摄氏度到华氏度转换

但它不起作用,我需要帮助

  1. f = Fahrenheit
  2. c = Celsius
  3. resultC =华氏温度转换为摄氏温度时显示的结果
  4. resultF = Celsius转换为Fahrenheit时出现的结果
  5. a =在摄氏度(C)/华氏度(F)之间切换
  6.         #include <iostream>
            using namespace std;
    
            char a;
            float c,f;
            float resultC, resultF;
    
            resultF = c*9.0/5.0+32.0;
            resultC = (f-32.0)*5.0/9.0;    
    
    
            int main () {
    
             cout << "Enter your operation (C/F):" << endl;
             cin >> a;
    
             switch (a) {
    
              case 'C':
                  cout << "Enter your number to convert Celsius to Fahrenheit:" 
               <<endl;
                      cin >> c;
    
                  cout << resultF << endl;
    
                  break;
    
              case 'F':
                  cout << "Enter your number to convert Fahrenheit to Celsius"                         
               <<endl;
    
                  cin >> f;
    
                  cout << resultC << endl;
    
                  break;
              }
    
           }
    

    当我运行程序时结果错误

    PS,我是C ++的新手,请不要这么认真

    感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

首先,您需要了解应避免使用全局变量

char a;
float c,f;
float resultC, resultF;

您得到错误答案的原因是因为您在实际获取向您提供垃圾值的用户输入之前计算resultFresultC。您需要先从用户那里获取输入,然后计算结果

#include <iostream>
using namespace std;

int main () {
    char a;
    float c,f;
    float resultC, resultF;

cout << "Enter your operation (C/F):" << endl;
 cin >> a;

 switch (a) {

  case 'C':
      cout << "Enter your number to convert Celsius to Fahrenheit"<<endl;
          cin >> c;
      resultF = c*9.0/5.0+32.0;
      cout << resultF << endl;
      break;

  case 'F':
      cout << "Enter your number to convert Fahrenheit to Celsius"<<endl;

      cin >> f;
      resultC = (f-32.0)*5.0/9.0;  
      cout << resultC << endl;
      break;
  }

}

答案 1 :(得分:1)

在读取输入之前,您正在进行转换计算。 您应该阅读输入,然后根据需要进行转换。

#include <iostream>
using namespace std;

char a;
float c,f;
float resultC, resultF;

int main () {

    cout << "Enter your operation (C/F):" << endl;
    cin >> a;

    switch (a) {

        case 'C':
            cout << "Enter your number to convert Celsius to Fahrenheit:" << endl;
            cin >> c;
            resultF = c*9.0/5.0+32.0;
            cout << resultF << endl;
            break;

        case 'F':
            cout << "Enter your number to convert Fahrenheit to Celsius"<<endl;                         
            cin >> f;
            resultC = (f-32.0)*5.0/9.0;
            cout << resultC << endl;
            break;
    }

}

您可以在上面的代码中看到,在读取输入后进行计算。

在您的代码中,您使用全局变量来存储结果和输入。这里没有必要,因为没有其他功能可以共享这些值。

您可以在main()中添加这些变量并将其设置为本地变量。您可以将它们设置为本地并重新编写代码。

#include <iostream>
using namespace std;



int main () {

    char a;
    float c,f;
    float resultC, resultF;

    cout << "Enter your operation (C/F):" << endl;
    cin >> a;

    switch (a) {

        case 'C':
            cout << "Enter your number to convert Celsius to Fahrenheit:" << endl;
            cin >> c;
            resultF = c*9.0/5.0+32.0;
            cout << resultF << endl;
            break;

        case 'F':
            cout << "Enter your number to convert Fahrenheit to Celsius"<<endl;                         
            cin >> f;
            resultC = (f-32.0)*5.0/9.0;
            cout << resultC << endl;
            break;
    }

}

因此,如果要在函数外声明变量,那么它是全局变量。也就是说,它的值可以被程序中的其他函数访问和更改。 如果变量在函数范围内声明,则它是该函数的本地变量。这意味着,该变量只能在该函数内部使用。

您可以按照此link了解详情并了解范围。

希望这有帮助。!