C++ Question (if and while loops)

For some reason it is closing whenever i go through the first statement completely or in the while loop if something is to happen if it is 0. I want it to display the pay message in both instances. however it crashes.

#include

using namespace std;

int main()
{
double wage;
double hours;
double tr;

cout << "Enter the wage for the employee" << endl;
cin >> wage;

if (wage >= 0.00001)
{
cout << "You have entered " << wage << " as a value" << endl;
cout << "Please enter the amount of hours the employee worked." << endl;
cin >> hours;
cout << "You have entered " << hours << "hours." << endl;
cout << "Please enter the tax rate as a decimal." << endl;
cin >> tr;
std::cout << "The employee's pay for this cycle is " << (wage * hours) - (wage * tr) << endl;

}

while (wage = 0)
{
cout << "Please reenter, you need to specify amount greater than 0.00." << endl;

if (wage >= 0.00001)
{
cout << "You have entered " << wage << " as a value" << endl;
cout << "Please enter the amount of hours the employee worked." << endl;
cin >> hours;
cout << "You have entered " << hours << "hours." << endl;
cout << "Please enter the tax rate." << endl;
cin >> tr;
std::cout << "The employee's pay for this cycle is " << (wage * hours) - (wage * tr) << endl;
}
}
return 0;
}

Respuesta :

tonb
Your while statement is in error

while (wage = 0) assigns 0 to wage.

What you want is to compare wage to 0, ie.:

while (wage == 0).

However, comparing double's to some value is very bad practice due to rounding errors. Much safer is to always have a < or > in there:

while (wage < 0.0001)

If you confuse assignment (=) and comparison (==) often, and you don't have a compiler to warn you for this, you can adopt the coding style to put the constant first:

while(0 == x)
ACCESS MORE