错误信息:
这是什么意思?
我该如何解决它?
错误 C2040:“==”:“int”与“const char [2]”的间接级别不同
Code:
#include <iostream>
#include <cmath>
using namespace std;
int round(double number);
//Assumes number >=0.
//Returns number rounded to the nearest integer.
int main()
{
double doubleValue;
char ans;
do
{
cout << "Enter a double value: ";
cin >> doubleValue;
cout << "Rounded that number is " <<round(doubleValue)<< endl;
cout << "Again? (y/n): ";
cin >> ans;
}
//Here is the line generating the problem, while(...);
while (ans == 'y' || ans == "Y");
cout << "End of testing.\n";
return 0;
}
//Uses cmath
int round(double number)
{
return static_cast<int>(floor(number + 0.5));
}
你需要单引号char
文字。您对第一个操作正确,但对第二个操作错误:
while (ans == 'y' || ans == "Y");
这应该是:
while (ans == 'y' || ans == 'Y');
双引号用于字符串 (const char[]
) 文字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)