c++ - "expected ';在返回语句之后,有人可以告诉我我的代码有什么问题吗?

标签 c++ compiler-errors

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

  • 在else块中,您编写了return cout "fine";(第41行)。将其更改为cout << "fine";
  • 您的代码中有未使用的变量“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++完全陌生,因此您现在不必担心。只需使用我向您展示的代码即可。

    关于c++ - "expected ';在返回语句之后,有人可以告诉我我的代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32878923/

    相关文章:

    c++ - 如何比较opencv中kepoints匹配的大小

    c++ - 为什么 std::num_put 通过非 const 引用获取 ios_base 参数?

    c++ - 更改按钮文本 winapi

    c++ - 如何删除 XML 中的节点及其子节点

    c - 如何让编译器识别对库的引用?

    c# - 较少严格的CodeDomProvider来编译DLL

    c++ - 如何将基本嵌套循环转换为 C++ 中的递归

    c# - WPF 中缺少 ListView.Columns

    compiler-errors - 在Bluej中无法取消引用Char的错误

    c# - 我不明白我做了什么