C ++错误C2040? [英] C++ Error C2040?

查看:948
本文介绍了C ++错误C2040?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误讯息:



这是什么意思?



p>

错误C2040:'==':'int'不同于'const char [2]'的间接层次



代码:

  #include< iostream> 
#include< cmath>
using namespace std;

int round(double number);
//假设数字> = 0。
//返回四舍五入到最接近的整数的数字。

int main()
{
double doubleValue;
char ans

do
{
cout< 输入双精度值:;
cin>> doubleValue;
cout<< 舍入该数字是<< round(doubleValue)<< endl
cout<< 再次?(y / n):;
cin>> ans;

}
//这是生成问题的行,while(...);

while(ans =='y'|| ans ==Y);

cout<< End of testing.\ n;

return 0;
}

//使用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 [] )literals。


Error Message:

What does this mean?

And how do I fix it?

error C2040: '==' : 'int' differs in levels of indirection from '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));
}

解决方案

You need to single-quote char literals. You did this correctly for the first one but not the second:

while (ans == 'y' || ans == "Y");

This should be:

while (ans == 'y' || ans == 'Y');

Double quotes are for string (const char[]) literals.

这篇关于C ++错误C2040?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆