R
rlueneberg
I am having trouble validating a social security number. If I use
valid integers, for example 3944 or 663949956, the code skips to loop2
and I don't have problems. If I use alpha characters, for example
"abcde" the code runs through loop1 and everything works perfectly.
The problem starts when I type in for example "44abcde". Immediately,
code enters in an infinite loop. If I type "abcde44" it works
normally. I am desperately looking for help since this is an
assignment and I am still learning the language.
int main()
{
HourlyPay p;
int SSN=0;
cout << "Enter Social Security: " << endl;
cin >> SSN;
// loop 1
while (cin.fail())
{
cin.clear();
cin.ignore(100, '\n'); // skip 100 chars or to next end of line
whichever comes first
cout << "Invalid Social Security Number!!!!\n";
cout << "Try Again: " << endl;
cin >> SSN;
}
// loop 2
while(p.getSSN(SSN)=="invalid")
{
cin.clear();
cout << "Invalid Social Security Number!!!!";
cin >> SSN;
}
cout << p.getSSN(SSN);
system("PAUSE");
return 0;
}
The other problem that I am having is if I type more than 100 alpha
chars it display another line repeating the same previous error
message.
Thank you
Rod
valid integers, for example 3944 or 663949956, the code skips to loop2
and I don't have problems. If I use alpha characters, for example
"abcde" the code runs through loop1 and everything works perfectly.
The problem starts when I type in for example "44abcde". Immediately,
code enters in an infinite loop. If I type "abcde44" it works
normally. I am desperately looking for help since this is an
assignment and I am still learning the language.
int main()
{
HourlyPay p;
int SSN=0;
cout << "Enter Social Security: " << endl;
cin >> SSN;
// loop 1
while (cin.fail())
{
cin.clear();
cin.ignore(100, '\n'); // skip 100 chars or to next end of line
whichever comes first
cout << "Invalid Social Security Number!!!!\n";
cout << "Try Again: " << endl;
cin >> SSN;
}
// loop 2
while(p.getSSN(SSN)=="invalid")
{
cin.clear();
cout << "Invalid Social Security Number!!!!";
cin >> SSN;
}
cout << p.getSSN(SSN);
system("PAUSE");
return 0;
}
The other problem that I am having is if I type more than 100 alpha
chars it display another line repeating the same previous error
message.
Thank you
Rod