【问题标题】:Invalid operands to binary expression error message二进制表达式错误消息的无效操作数
【发布时间】:2014-12-24 02:34:43
【问题描述】:

我真的是编程新手(我正在学习 C++)。有人可以告诉我为什么在尝试运行这段代码时收到此错误消息。

int main()
{
    auto days=0, hours_worked=0;

    cin  >> "days"; // This is where I get the error message.
    cout << "Days worked per week";

    cin  >> "hours_worked"; // This is where I get the error message.
    cout << "Hours worked per day";

    cout << "This week Paul worked: "
         <<"6*9"<< endl;

    return 0;
}

【问题讨论】:

  • 去掉变量名的引号。

标签: c++ binary expression message operands


【解决方案1】:
#include <iostream>

using namespace std; //we are going to use std::cin, std::cout, std::endl from the header file <iostream>

int main()
{
    int days=0, hours_worked=0; //why not just declare it as integer?

    cin  >> days; //you need to write it without "" otherwise its treated as a string and not a variable
    cout << "Days worked per week" << days; //no. of days the person worked

    cin  >> hours_worked; // same here
    cout << "Hours worked per day" << hours_worked;

    cout << "This week Paul worked: "
         << (days*hours_worked) << " hours" << endl; //paul worked (days*hours_worked) hours

    return 0;
}

是更正后的代码。希望您理解更正。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多