Hi all. I am having trouble with these inside a loop I have. The loop is used to fill a struct and I want to check each input before it is accepted. It works fine if the input is what is expected but doesnt if it is incorrect input. Here is my code:

for (int i = 0; i <= 200; i++){

    cout << "Enter employee ID: " << endl;
	cin >> sheet[i].empID;
    } while (isalpha(sheet[i].empID || !isdigit(sheet[i].empID)));
//it carrys on with the next cout/cin if input is correct but if its wrong
//it shoots down to where I will comment next

	cout << "Enter last name: " << endl;
	cin >> sheet[i].lastName;
	cout << "Enter first name: " << endl;
	cin >> sheet[i].firstName;
	cout << "Enter department no.: " << endl;
	cin >> sheet[i].dept;
	cout << "Enter hours worked: " << endl;
	cin >> sheet[i].hours;
        if (sheet[i].hours > 37.5) {
        cout << "Enter over time worked: " << endl;
        cin >> sheet[i].overTime;
        else {sheet[i].overTime = 0;}
	cout << "Enter hourly rate: " << endl;
	cin >> sheet[i].rate;


//here and then is unresponsive
cout << "1: Enter another record" << endl;
cout << "2: Return to Main Menu" << endl;
cout << "3: Exit program" << endl;
cin >> enterNext;
    if (enterNext == 1) { rows++;}
    else if (enterNext == 2) {break;}
    else if (enterNext == 3) {exit(1);}
    else {system("CLS"); goto check;}

Any ideas why or any suggestions of another way I could do this? Thanks for your time!

6 Years
Discussion Span
Last Post by pseudorandom21

So - You want to add 200 employees to the system, right? Because in the and You have option to add another record - so I don't get it - You know how many employees You want to add or not?

And in the end - try to use switch instead of so many if & else.

How correct data should look like?


200 is just an arbitrary number, the point is that the number of entries may vary. I don't see why switch would be superior to if and else on this occasion though? Any reason why?

I'm really just playing about with isdigit()/isalpha() to see how to make them work. I don't really need to check this data. Just curious.


I assume that you want the following

while (isalpha(sheet[i].empID || !isdigit(sheet[i].empID)))

to be

while (isalpha(sheet[i].empID) || !isdigit(sheet[i].empID))


Another way of checking the input is to use formatted input with cin >>.
You can enable exceptions by doing the following:

cin.exceptions(istream::failbit | istream::badbit);

Then you can simply do this:

int val;
try {
  cout << "Input a number: ";
  cin >> val;
catch (exception e) {
  cout << "Error: Not a number, try again!" << endl;
  cin.ignore(200, '\n');

Or you can use cin.fail() instead of using exceptions to check for errors.


I fixed my typo in the do while loop but it's still stuck. I'll give exceptions and cin.fail a go now. Thanks!

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.