Hi. I'm starting my C++ 2 class this coming semester. I figured I would play around with some basics to review before starting. I made this super basic application to tell a student what grade they made. If I type in a number of any sorts, the program works. If I type in a letter, it tells the student that they received an "F" instead of "Invalid Input" like I would like it to. Can anyone help me find the error with my else statement?
I altered it to where there is a limit of 90 - 100 on getting an A. If I type '101' it will be invalid. I know for sure the code runs, but I just want to be able to limit the input to a number.
Line 14 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
This Is just a call to istream::ignore(firstParam, delimiter). This call ignores the number of characters specified by firstParam up to and including delimiter. The problem with this call is that we don't know before hand the size of the stream (sd::cin in this case), and we would like to ignore the entire stream up to and including the '\n'.
std::numeric_limits<std::streamsize>::max(),
This portion of the code results in an integer the value of which is equivalent to the size of the stream. This std::numeric_limits<std::streamsize>:: just specifies a namespace. max() is the name of a static function . Which returns the maximum size of the stream.
[Edit]
it would be more accurate to say that max returns the maximum size of its template parameter. Which in this case happens to be std::streamsize.
I understand the cin.ignore part. So what I'm getting is that it's saying "Read up to as many integers as the user enters or until a \n is found." Is that basically it? Thanks for the help by the way. I've not ever explored any libraries outside cmath, iomanip, and iostream.
The conditions from lines 12-32 cover all possible values for studentGrade. That's why the else never executes. If you enter 101, then line 12 (studentGrade > 89) is true.
if (studentGrade > 89) all greater than 89 is trueelseif (studentGrade < 60) // all less than 60 is true
elseif (studentGrade > 79 && studentGrade <= 89) // 89 is true
elseif (studentGrade > 59 && studentGrade <= 69) // 60 is true
base on your conditions.
i dont think theres a possibility that the else statement would be executed