The user will always press Enter after an input.
Hence, read the user's input as a string, using
std::getline().
1 2 3 4 5 6 7 8
|
cout << "Please enter your name. ";
getline(cin, sUserName);
cout << "\nPlease enter the amount of credit hours you are taking. ";
cin >> iCreditHrs;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cout << "\nPlease enter either the letter grade you would like to receive\n";
cout << "or the number of hours you would like study each week. ";
getline(cin, sInput);
|
Now all you have to do is check to see if
sInput is a letter (in A..F). Failing that, attempt to convert it to a float with a
std::stringstream.
1 2 3 4 5 6 7 8 9
|
istringstream ss( sInput );
ss >> fStudyHrs;
if (!ss.eof()) {
cout << "That's neither a letter grade nor a number of hours to study."
//loop to try again
}
if (fStudyHrs < 0.0) {
...
}
|
Also, your loops are off. The purpose of the loop is to give the user an opportunity to correct his input. If there is no input in the loop, then user cannot do that.
1 2 3 4 5
|
while (true) {
cout << "\nPlease enter either the letter grade you would like to receive\n";
cout << "or the number of hours you would like study each week. ";
}
getline(cin, sInput);
|
Oh, and one last thing -- it's a stylistic issue but in this case a fairly good one: don't use MS "Systems Hungarian Notation".
Those prefixes that tell you the data type of your variable are all but useless, and make maintenance a nightmare.
Read here for more (and enlightenment):
http://www.joelonsoftware.com/articles/Wrong.html
Hope this helps.