tags:

views:

187

answers:

4
+2  Q: 

C++ Error C2040?

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));
}
+10  A: 

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.

tgamblin
Thank you, I need to start paying more attention to all small details in C++.
You could try this: `while (toupper(ans) == 'Y');`
Thomas Matthews
+1  A: 

You have double quotes instead of single ones on this line:

while (ans == 'y' || ans == "Y");
avakar
+2  A: 

The capital Y is contained in double quotes, which creates a const char [2] (Y followed by null). You probably ment:

while (ans == 'y' || ans == 'Y');
e8johan
A: 

I dont know this is useful or not but it may be like following:

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

Pranjali
The semi-colon is required because this is the end of a do-while-statement (instead of being a while-statement).
Roger Pate
ok sorry i have not seen it is a do while statement. ok
Pranjali