Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error C2040?

Tags:

c++

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));
}
like image 768
user242229 Avatar asked Nov 20 '25 09:11

user242229


1 Answers

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.

like image 178
Todd Gamblin Avatar answered Nov 21 '25 22:11

Todd Gamblin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!