我尽了最大的努力,但无法弄清楚我的代码出了什么问题。
我收到错误代码"error: expected ' ; ' after return statement."
注意:这是我除了“hello world”之外的第一个程序,我们将不胜感激。
#include <iostream>
using namespace std;
int main() {
int celsius, fahrenheit;
//Get degrees in celsius
cout << "Please input degrees Celsius: \n";
cin >> celsius;
//convert celsius to fahrenheit
fahrenheit = celsius * 1.8 + 32;
//display degrees farhenheit/ thank you message
cout << "\n" << "Degrees Fahrenheit: \n" << fahrenheit << "\n";
cout << "\n" << "Thank you for using my Celsius to Fahrenheit converter. \n" << "\n";
{
int yes = 1, no = 0;
cout << "do you wish to convert again? \n";
cin >> yes;
if (yes == 1) {
return cout << "please enter degrees Celsius" ;
cin >> celsius;
//convert celsius to fahrenheit
fahrenheit = celsius * 1.8 + 32;
cout << "\n" << "Degrees Fahrenheit: \n" << fahrenheit << "\n";
} else {
return cout "fine";
}
}
return 0;
}
最佳答案
好吧,您在代码中犯了3个错误:
1.在if块中,您已经编写了return cout << "please enter degrees Celsius" ;
(第31行)。但是cout不会返回任何内容(有关详细信息,请参见下面的P.S.)。将其更改为cout << "please enter degrees Celcius";
return cout "fine";
(第41行)。将其更改为cout << "fine";
您的最终代码应如下所示:
#include <iostream>
using namespace std;
int main()
{
int celsius, fahrenheit;
//Get degrees in celsius
cout << "Please input degrees Celsius: \n";
cin >> celsius;
//convert celsius to fahrenheit
fahrenheit = celsius * 1.8 + 32;
//display degrees farhenheit/ thank you message
cout << "\n" << "Degrees Fahrenheit: \n" << fahrenheit << "\n";
cout << "\n" << "Thank you for using my Celsius to Fahrenheit converter. \n" << "\n";
int yes = 1;
cout << "do you wish to convert again? \n";
cin >> yes;
if (yes == 1)
{
cout << "please enter degrees Celsius" ;
cin >> celsius;
//convert celsius to fahrenheit
fahrenheit = celsius * 1.8 + 32;
cout << "\n" << "Degrees Fahrenheit: \n" << fahrenheit << "\n";
}
else
{
cout << "fine";
}
return 0;
}
附言运算符“<<”确实返回一个
std::ostream
对象cout
。该对象可以转换为bool,但不能转换为int。但是,由于您对C++完全陌生,因此您现在不必担心。只需使用我向您展示的代码即可。
关于c++ - "expected ';在返回语句之后,有人可以告诉我我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32878923/