Repeating

I'm stuck on a little issue that is most likely right in my face, yet I am oblivious.

I'm working on a "Numerical to Letter Grade Converter" for practicing purposes.

When typing letters rather than numbers, my else statement does not pop up. As well, repeats the else if statement above it.

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
#include <iostream>

//@author Will A. Training Exercise:Numerical to Letter Grade Converter

int main()
{
	int grade = 0;
	bool repeat = true;

	std::cout<<"Grade Converter Created By Will A.\n";

	while(repeat)
	{
		std::cout << "\nEnter Grade(numerical): ";
		std::cin >> grade;

		if((grade <=100)&&(grade >=90))
		{
			std::cout<<"Your Letter Grade:A\n";
		}
		else if((grade <=89)&&(grade >=80))
		{
			std::cout<<"Your Letter Grade:B\n";
		}
		else if((grade <=79)&&(grade >=70))
		{
			std::cout<<"Your Letter Grade:C\n";
		}
		else if((grade <=69)&&(grade >=65))
		{
			std::cout<<"Your Letter Grade:D\n";
		}
		else if((grade <=64)&&(grade >=0))
		{
			std::cout<<"Your Letter Grade:F\n";
		}
		else
		{
			std::cout<<"You did not enter a correct number\n";
		}
	}
} 
Last edited on
Because grade is holding what you're typing in and grade is of type int.
A letter is just an ascii value, so grade is holding the ascii int value of the character you entered.
How do you prevent it from breaking.


Do you know how to fix it?
Lots of ways to fix it, you could get user input and then check if it's digits before changing it to an int. You could do that using ascii values, If val is a digit then int = val - '0'.
You could play around with stringstream also.
You could write your own function to check if every value in a string is a digit. Like with a for loop and a string iterator and the function isdigit.
Topic archived. No new replies allowed.