Else Statement not Running

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>

using namespace std;

int main()
{
int studentGrade;

    cout << " What score did you recieve?" << endl;
    cin >> studentGrade;

    if (studentGrade > 89){

        cout << "You got an A. Congrats.";
    }

    else if (studentGrade > 79 && studentGrade <= 89){

        cout << "You got a B. Congrats.";
    }

    else if (studentGrade > 69 && studentGrade <= 79){

        cout << "You got an C. Better luck next time.";
    }

    else if (studentGrade > 59 && studentGrade <= 69){

        cout << "You got a D. You almost goofed that up.";
    }

    else if (studentGrade < 60){

        cout << "You got an F. Have fun flipping burgers.";
    }

    else {
    cout << "Invalid input.";
    }

    return 0;
}

How would I get it to make a char an invalid input?
char data type can be converted to int, so there isn't an error. Your else statement will never execute so there is no point in having it.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <limits>
using namespace std;

int main()
{
	int studentGrade;

	cout << " What score did you recieve?" << endl;
	
	while (!(std::cin >> studentGrade))
	{
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		std::cout << "Invalid input please try agian " << std::endl;
		cout << " What score did you recieve?" << endl;
	}


	

	if (studentGrade > 89){

		cout << "You got an A. Congrats.";
	}

	else if (studentGrade > 79 && studentGrade <= 89){

		cout << "You got a B. Congrats.";
	}

	else if (studentGrade > 69 && studentGrade <= 79){

		cout << "You got an C. Better luck next time.";
	}

	else if (studentGrade > 59 && studentGrade <= 69){

		cout << "You got a D. You almost goofed that up.";
	}

	else if (studentGrade < 60){

		cout << "You got an F. Have fun flipping burgers.";
	}

	else {
		cout << "Invalid input.";
	}


	return 0;
}
Could you explain line 14? I've never seen that before.
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.
Last edited on
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.
it's saying "Read up to as many integers as the user enters or until a \n is found


Yes but it ignores the characters
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.
1
2
3
4
if (studentGrade > 89)  all  greater than 89 is true
else if (studentGrade < 60) // all less than 60 is true
else if (studentGrade > 79 && studentGrade <= 89) // 89 is true 
else if (studentGrade > 59 && studentGrade <= 69) // 60 is true  

base on your conditions.
i dont think theres a possibility that the else statement would be executed

Topic archived. No new replies allowed.