I don't know why it turns into an Infinite loop.

The while loop in the beginning of my code is my verification loop and it works when a number is entered that is not 1-3 but when I enter a character it turns into an infinite loop and I have no idea why.

#include <iostream>
using namespace std;

int main ()
{
int choice;
double totalhours, totalpay, payrate;


cout << "Do you want to calculate you're" << endl
<< "1. Rate." << endl << "2. Total Hours" << endl << "3. Total Pay" << endl;
cout << "Please enter the number." << endl;
cin >> choice;

while (choice < 1 || choice > 3)
{
cout << "Invalid. Please enter one of the options " << endl;
cin >> choice;
}




if (choice == 1)
{
cout << "Please enter your total pay." << endl;
cin >> totalpay;
cout << "Please enter your total hours worked." << endl;
cin >> totalhours;

payrate = totalpay / totalhours;

cout << "Your pay rate is " << payrate << endl;
}

else if (choice == 2)
{
cout << "Please enter your total pay." << endl;
cin >> totalpay;
cout << "Please enter your pay rate." << endl;
cin >> payrate;

totalhours = totalpay / payrate;

cout << "You have worked a total of " << totalhours << " hours " << endl;
}

else if ( choice == 3)
{
cout << "Please enter your total hours worked " << endl;
cin >> totalhours;
cout << "please enter your pay rate " << endl;
cin >> payrate;

totalpay = totalhours * payrate;

cout << "Your total pay is $" << totalpay << endl;
}





return 0;
}
a) please use code tags
b) using namespace std; is bad
c) i would look into switches instead of an if chain like that
d) the solution to your problem is you need and (&&) instead of or(||)
You need to understand how input works.
When you enter something and press enter, that line is sent to the input buffer.
Actual input operations like >> are working on input buffer. If input buffer is empty, program request you to enter another line.

When you trying to enter character into int, it sees an char, sets stream into failed state and does not performs any further extractions leaving offending symbol in buffer. After that loop checks is performed and as value is not changed it loops again. But input buffer still not empty, so program is trying to read value without asking you for input and encounters same character again. Repeat.

Uou need to check stream state after input and clear error flags and input buffer if needed.

EDIT: Little Bobby Tables your d point is invalid. Actually it would make loop useless.
Last edited on
im sorry you are correct. i was rushing a bit and misread how he wrote it. normally i do it where it checks if it is inside the range not outside of it, which is what he had done here. also, he is correct about the char. you would have to write an input function and then verify it is the correct type, using sstream, if you dont want it to crash like that. you can also clear the buffer
A quick code snippet to ensure correct value is entered:
1
2
3
4
5
6
std::cout << "enter a number between 1 and 3: ";
while( !(std::cin >> value) ) {
    std::cout << "Wrong input, try again: ";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
After that you can check if it in range you need.
Topic archived. No new replies allowed.