Problem with IF statement.

Hello everyone,

I am writing this program which will allow you to calculate kinetic energy via it's formula. But I have stumbled across a problem.
I use double variables to get an input from the user. This variable get's assigned by the user with any number to their choice OR a question mark (?). BUT when I use that double variable in an IF statement to check if they inputted a number or a ? I get an error. I don't have a clue on how to solve this. Can someone please help me out?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int main() {

    double InputEK;
    double InputM;
    double InputV;
    double sum_EK;
    double sum_M;
    double sum_V;
    double total;
    string if_Correct;


    if_Correct = "N";

    while (if_Correct == "N") {

        system("cls");
        printIntro();
        printData();

        cout << "Info:  Ek = ";
        cin >> InputEK;
        cout << "       m  = ";
        cin >> InputM;
        cout << "       v  = ";
        cin >> InputV;
        cout << endl << endl;
        cout << "Formula: " << endl;
        cout << "Ek = 1/2 * m * v^2" << endl;
        cout << InputEK << " = 1/2 * " << InputM << " * " << InputV << "^2" <<  endl;
        cout << endl;
        if (InputEK == "?") {
            cout << "You want to calculate 'Ek' with m= " << InputM << " & v= " << InputV << endl;
            }
        if (InputM == "?") {
           cout << "You want to calculate 'm' with Ek= " << InputEK << " & v= " << InputV << endl;
            }
        if (InputV == "?") {
           cout << "You want to calculate 'v' with m= " << InputM << " & Ek= " << InputEK << endl << endl;
            }


I get the error at the IF statements at the bottom of the code.
The error says:
error: invalid operands of types 'double' and 'const char [2]' to binary 'operator=='|
Your variables InputEK, InputM, and InputV are of type double (aka, real numbers). As far as I know, "?" is not a real number.
Last edited on
Hi LB

Haha you are right there, but if I use these as a string instead of a double I cannot hook up a sum to them. Because then it is going to say that std::string can't be converted to double...
I think I see what you're trying to do here. For each value, you can accept it as a string and then try to convert it to a double - if that conversion fails, you know it as probably not a real number:
1
2
3
4
5
6
7
8
9
10
11
std::string value_str;
std::cin >> value_str;
double value = 0.0;
if(std::istringstream(value_str) >> value)
{
    //it was a real number
}
else
{
    //it was not a real number
}
I recommend some sort of utility function to reduce code duplication.
Last edited on
The problem is you're trying to use your inputs as both strings and doubles.

Here's one way:
1
2
3
4
5
6
7
8
9
10
11
  string InputV;
  double v = 0;

  cin >> InputV;
  if (InputV == "?")
  {  // Deal with the ?
  }
  else
  {  // Assume it's numeric.  Convert to double
    v = strtod (InputV.c_str(), nullptr);
  }

Will this line: v = strtod (InputV.c_str(), nullptr); allready convert it to double? Or would I have to add something?
Yes that line will convert the string to double. http://www.cplusplus.com/reference/cstdlib/strtod/

Note that strtod will return 0.0 if there is a conversion error.
That may or may not be sufficient to determine if there was a conversion error.

LB's approach is preferred because you can detect that the input string was not converted to a double.
Okay thank you very much, but I don't know what I would have to do with this:
1
2
3
4
std::string value_str;
std::cin >> value_str;
double value = 0.0;
if(std::istringstream(value_str) >> value)


*Sorry for my confusion, I am still a beginner..
Referring to LB's post, replace line 6 with whatever you want to do with the value. Line 4 has successfully put the double value in the variable called value. The fact that you're at line 6 indicates the conversion of the input to a double was successful. You might not need to do anything at all.

Line 10 is where you want to test if is value_str == "?". Line 4 has failed to convert the input to a double. You of course have the situation that you have to deal with where the input is a string, but is not "?".
Ah okay I get it now thanks a lot!!!!!!!!!!
Topic archived. No new replies allowed.