gpt4 book ai didi

C++ 错误 2679 : binary'>>' : no operator found

转载 作者:行者123 更新时间:2023-11-28 00:11:43 27 4
gpt4 key购买 nike

我在构建程序时收到以下错误消息:

errorC2657: binary '>>': no operator found which takes a right hand operand of type'const char[2]'(or there is no acceptable conversion)

这是我的程序的源代码:

#include <iostream>
#include <string>

using namespace std;

int main()
{
int number1;
int number2;
int intSum;
int intdiff;
int intprod;
int intaver;
cout << "Enter the 1st and 2nd ODD integers (separate them by a space): ";
cin >> number1 >>" " >> number2;
intSum = abs(number1 + number2);
intdiff = abs(number1 - number2);
intprod = abs(number1 * number2);
intaver = abs((number1 + number2) / 2);
if ( number1%2 != 0)
cout << "You must enter ODD integers! See you next time." <<endl;
else if ( number2%2 != 0)

cout << "You must enter ODD integers! See you next time." <<endl;
else
{
cout << "The sum of the integers is"<<intSum<<endl;
cout << "The difference of the integers is"<<intdiff<<endl;
cout << "The product of the integers is"<<intprod<<endl;
cout << "The average of the integers is"<<intaver<<endl;
if (number1 > number2)
cout << "The first one "<< number1 <<" is bigger." <<endl;
else if (number2 > number1)
cout << "The second one "<< number2 <<" is bigger." <<endl;
else
cout <<"Both numbers are the same."<<endl;
}
return 0;
}

请帮我解决问题。我是初学者。

最佳答案

这个声明:

cin >> number1 >> " " >> number2;

尝试将您的输入写入number1,然后是字符串文字(const char[2] ),然后是 number2。这不是你想要的,特别是因为流足够聪明,可以自己跳过空白:

cin >> number1 >> number2;

你可以用下面的代码看到这一点:

#include <iostream>
int main() {
int i1, i2;
std::cout << "Please enter two integers: ";
std::cin >> i1 >> i2;
std::cout << "You entered " << i1 << " and " << i2 << '\n';
return 0;
}

它在运行时是这样工作的:

Please enter two integers:     4     77
You entered 4 and 77

关于C++ 错误 2679 : binary'>>' : no operator found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731510/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com