samedi 23 mai 2020

If statement will not run in do - while loop

Below is a demonstration of an issue I'm having.

The if statement in the do - while loop will not run. Even though the logic for the "while" is exactly the same and executes correctly. I've stepped through the issue with the debugger and the value of testVar is -1.00000000 and so on.

#include<iostream>
using namespace std;


int main()
{
    double testVar = 0.00;

    do
    {
        cout << "Enter variable " << endl;
        cin >> testVar;
        if (testVar < 0.00) { "Yep!"; } **//Doesn't work.**

    } while (testVar < 0.00); **//Works**

    return 0;
}
The output of running this program looks like this:
Enter variable
-1
Enter variable
-1
Enter variable
-1
Enter variable
-1

Can someone please explain this behavior and offer a solution? I feel like I'm taking crazy pills because I should see "Yep!" when testVar = -1.00.

BTW I've developed in C# and Delphi. I'm currently taking C++ for a degree requirement.

Aucun commentaire:

Enregistrer un commentaire