【发布时间】: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 背后的主脑?
-
您在输出值后调用您的函数。此外,您可能希望使用传递给您的函数的值(
tempInFarenheit和tempInCelsius)。 -
你确定你没有困惑吗?
fToC返回 farenheit,而命名则暗示其他内容。CtoF也是如此。正如一些已经指出的那样,您正在使用未使用的变量。