Im trying to carry out input validation on a piece of code and have come across an issue.
Basically for the 1st input I used getline for the string and then for the 2nd input I use cin for the integer. I am aware you can convert the integer from a string with getline but I wish to use the validation underneath to cover for the fact that a user may enter a string instead of the required integer.
When this code runs the second revolution of the For Loop it fires out the first two requests without waiting for user input. I suspect its the clash with getline and cin but Im unsure how to resolve except for using getline which nullifies the validation code.
Any ideas?
If you need any more clarification on my long winded, tedious problem just ask.
Cheers.
int main ()
{
int n,x,numb;
string mystr;
for (n=0; n<N_TEAMS; n++)
{
cout << "Enter Seeded Team League : ";
getline (cin,seeded[n].stdetails.league, '\n');
cin.clear();
cout << "Enter Seeded Team League Placing : ";
cin >> seeded[n].stdetails.numb;
cin.ignore(numeric_limits<int>::max(), '\n');
if (!cin || cin.gcount() != 1)
{
cout << "Not a numeric value.";
cin.ignore(numeric_limits<int>::max(), '\n');
}
else
{
cout << "Your entered number: " << seeded[n].stdetails.numb <<endl;
cin.ignore(numeric_limits<int>::max(), '\n');
}
cout <<"\n";
system("Pause");
system("cls");
}
for (n=0; n<N_TEAMS; n++)
{
cout << "League is " << seeded[n].stdetails.league << endl;
cout <<"\n";
cout << "Numb is " << seeded[n].stdetails.numb << endl;
system("Pause");
cout <<"\n";
}
}