【问题标题】:"expected ' ; ' after return statement, could someone tell me what is wrong with my code?“预期 ' ; ' 在 return 语句之后,有人可以告诉我我的代码有什么问题吗?
【发布时间】:2015-10-01 03:22:45
【问题描述】:

我尽了最大努力,但无法弄清楚我的代码出了什么问题。 我收到一个错误代码"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;
}

【问题讨论】:

  • 你也有不必要的大括号{ }

标签: c++ compiler-errors


【解决方案1】:

好吧,你在代码中犯了 3 个错误:

1.在 if 块中你写了return cout &lt;&lt; "please enter degrees Celsius" ;(第 31 行)。但是 cout 不返回任何内容(有关详细信息,请参阅下面的 P.S.)。改成cout &lt;&lt; "please enter degrees Celcius";

  1. 在 else 块中你写了return cout "fine";(第 41 行)。改成cout &lt;&lt; "fine";

  2. 您的代码中有未使用的变量 "no"。它仅存在但不参与代码。删除该变量。

您的最终代码应如下所示:

#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++ 完全陌生,因此您暂时不必担心这一点。只需使用我向您展示的代码即可。

【讨论】:

  • cout 不返回任何东西,我想,这是真的,因为cout 是一个东西,而不是一个运算符。运算符是&lt;&lt;,它确实返回一些东西;这里使用的重载(std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const char*))返回一个流;具体来说,与用作左侧操作数的流相同。所以cout &lt;&lt; "foo" 返回coutstd::ostream 对象可以转换为bool 值(如果流可以写入,则转换为真)但它们不能转换为int。所以声明是不正确的(并且返回不希望的)但不是因为你给出的原因。
  • 我在回答中的意思是 cout 不会返回温度值。它只是在他的屏幕上显示价值。我只是不想通过告诉他有关 ostream 对象和运算符重载的事情来迷惑他。所以我只是告诉他从这些语句中删除“return”并在 cout 之后使用“
  • 不混淆的愿望是高尚的,但会助长流行的误解,即以cout &lt;&lt; 开头的东西是“cout 声明”造成混淆。 OP 可能不明白 return 做了什么,但你的回答也不能促进这种理解。
  • 好吧,我刚刚添加了一个 P.S.声明“
【解决方案2】:

删除cout之前的两个垃圾return并在cout"fine"之间添加&lt;&lt;

【讨论】:

    【解决方案3】:
    return cout << "please enter degrees Celsius" ; //line 31
    return cout "fine"; //line 41
    

    从两个语句中删除返回。

    cout 是ostream 类的对象。您可以阅读更多关于cout here 的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-21
      • 2020-10-30
      • 2010-11-08
      • 1970-01-01
      • 2021-11-16
      相关资源
      最近更新 更多