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

我尽了最大的努力,但无法弄清楚我的代码出了什么问题。
我收到错误代码"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++完全陌生,因此您现在不必担心。只需使用我向您展示的代码即可。

    https://stackoverflow.com/questions/32878923/

    相关文章:

    swift - 类型 'Category'(aka 'OpaquePointer')的值没有成员 '

    c++ - 以下代码段出了什么问题?

    ruby-on-rails-3 - 在命名空间模型上使用 factory_girl_rails 和

    c# - 如果其他语句不起作用

    objective-c - ARC 不允许将 'int' 隐式转换为 'id _Nonnull'

    c# - 无法将类型int隐式转换为int数组

    java - 我的method.add(int)不会将用户输入添加到我的数组中

    java - 为什么此代码段显示编译错误?

    c++ - 长双二维动态数组C++

    c# - 为什么编译器认为Environment.Exit可以返回?