【问题标题】:C++ Program is returning "0" every time?C++程序每次都返回“0”?
【发布时间】:2017-09-26 18:20:59
【问题描述】:

我的输出示例:https://imgur.com/a/nAU41 我正在尝试制作一个简单的 F 到 C 构造函数类型程序。不过,每次我运行它时,我都会得到 0 的响应。人们对这种类型的程序提出的许多问题都与他们没有将他们的(5/9)加倍(5/9)我已经这样做但仍然得到回报有关'0'。

    /* Mr. Gadaffi
 04.27.17 farenheit to celsius program
 */

#include <iostream>
using namespace std;

// 1.8 * cel + 32;
// 0.55555 * (far - 32);

double fToC(double tempInFarenheit) {
    double farenheit, celsius;
    farenheit = 1.8 * (celsius + 32);
    return farenheit;
}

double cToF(double tempInCelsius) {
    double farenheit, celsius;
    celsius = (5/9.0) * (farenheit - 32);
    return celsius;
}

int main () {

    string typeOfDegrees;
    double farenheit, celsius;

    cout << "Please enter whether your initial degrees are in Farenheit or Celcius(F or C): " << endl;
    cin >> typeOfDegrees;

        if (typeOfDegrees == "F") {
            cout << "What is the degrees in Farenheit?: " << endl;
            cin >> farenheit;
            cout << "The temperature in Celsius is: " << celsius << " degrees C" << endl;
            celsius = fToC(farenheit);
    }
    else if (typeOfDegrees == "C")
    {
        cout << "What is the degrees in Celsius?: " << endl;
        cin >> celsius;
        cout << "The temperature in Farenheit is: " << farenheit << " degrees F" << endl;
        farenheit = cToF(celsius);
    }

    return 0;
}

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programs
  • 调试时看到了什么? Nicholas Colan 是 M. Gaddaffi 背后的主脑?
  • 您在输出值后调用您的函数。此外,您可能希望使用传递给您的函数的值(tempInFarenheittempInCelsius)。
  • 你确定你没有困惑吗? fToC 返回 farenheit,而命名则暗示其他内容。 CtoF 也是如此。正如一些已经指出的那样,您正在使用未使用的变量。

标签: c++ xcode


【解决方案1】:

在 C/C++ 中,指令是按顺序执行的。这意味着,在函数内部,第 X 行在第 X+1 行之前完成。你的情况

double celcius;

// ...

cout << "The temperature in Celsius is: " << celsius << " degrees C" << endl;
celsius = fToC(farenheit);

因此,您首先将 celcius 发送到 cout,然后然后计算 celcius。您可能想切换这些行:

celsius = fToC(farenheit);
cout << "The temperature in Celsius is: " << celsius << " degrees C" << endl;

摄氏 -> 华氏转换也是如此。

((我知道很多 SO 人喜欢谈论“未定义的行为”,无论提问者是否关心那是什么。所以我会提到,作为一个“优秀的 SO 用户”,使用没有的变量t 被初始化是未定义的行为。))

另外,您的函数 cToFfToC 不使用参数值。

【讨论】:

    【解决方案2】:
    double fToC(double tempInFarenheit) { // you forget to use your variable
        double farenheit, celsius; // you create celsius here
        farenheit = 1.8 * (celsius + 32); // so that is 1.8 * (undefined + 32)
        return farenheit;
    }
    
    double cToF(double tempInCelsius) { // Same here
        double farenheit, celsius;
        celsius = (5/9.0) * (farenheit - 32);
        return celsius;
    }
    

    那应该做你想做的:

    double fToC(double tempInFarenheit) {
        double farenheit, celsius;
        farenheit = 1.8 * (tempInFarenheit+ 32);
        return farenheit;
    }
    
    double cToF(double tempInCelsius) {
        double farenheit, celsius;
        celsius = (5/9.0) * (tempInCelsius- 32);
        return celsius;
    }
    

    还有:

      if (typeOfDegrees == "F") {
            cout << "What is the degrees in Farenheit?: " << endl;
            cin >> farenheit;
            cout << "The temperature in Celsius is: " << celsius << " degrees C" << endl; // you send the message here out
            celsius = fToC(farenheit); // and here you are calculating it... that can't work
    

    这个:

      if (typeOfDegrees == "F") {
                cout << "What is the degrees in Farenheit?: " << endl;
                cin >> farenheit;
                celsius = fToC(farenheit);
                cout << "The temperature in Celsius is: " << celsius << " degrees C" << endl;
    }
    

    【讨论】:

    • 您可以使功能更加简单。只需在其中添加 return 1.8 * (tempInFarenheit+ 32);return (5/9.0) * (tempInCelsius- 32);
    • @NathanOliver 当然,可以做更多,但我想保持简单;)
    【解决方案3】:

    仅供参考,以上可以缩短:

    double fToC(double tempInFarenheit) {
        return (5/9.0) * (tempInFarenheit - 32);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 2015-08-21
      • 2019-02-26
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多