need help with problem

The problem is with "case '10'". When I debug and type in "10", result comes back as "You entered I". How can I fix this? Here is the code:

// Roman Numeral Converter, 1-10
#include<iostream>
using namespace std;

int main()
{
char number; // "char" is used instead of "int"

cout << "Please enter a number from 1 through 10: \n";
cin >> number;

// Use switch statement
switch (number)
{
case '1' : cout << "You entered I.\n";
break;
case '2' : cout << "You entered II.\n";
break;
case '3' : cout << "You entered III.\n";
break;
case '4' : cout << "You entered IV.\n";
break;
case '5' : cout << "You entered V.\n";
break;
case '6' : cout << "You entered VI.\n";
break;
case '7' : cout << "You entered VII.\n";
break;
case '8' : cout << "You entered VIII.\n";
break;
case '9' : cout << "You entered IX.\n";
break;
case '10' : cout << "You entered X.\n"; // Result says "You entered I"
break;

default: cout << "You did not enter a number from 1 through 10.\n"; //Input Validatition does not work
}


system ("pause");
return 0;

}
Last edited on
Hi, the reason when you enter 10, and it states you enter 1, is because you are using char instead int. Using char, it reads the first character of the input, for this case, the first character for '10' is '1'.

Any reason not using int? Simply change it to int and it solves all the problem. :)

Below is the code after changing it to int. Hope this helps!!!
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
int main()
{
	int number; // "char" is used instead of "int"

	cout << "Please enter a number from 1 through 10: \n";
	cin >> number;

	// Use switch statement
	switch (number)
	{
		case (1) : cout << "You entered I.\n";
		break;
		
		case (2) : cout << "You entered II.\n";
		break;
	
		case (3) : cout << "You entered III.\n";
		break;
		
		case (4) : cout << "You entered IV.\n";
		break;
		
		case (5) : cout << "You entered V.\n";
		break;
		
		case (6) : cout << "You entered VI.\n";
		break;
		
		case (7) : cout << "You entered VII.\n";
		break;
		
		case (8) : cout << "You entered VIII.\n";
		break;
		
		case (9) : cout << "You entered IX.\n";
		break;
		
		case (10) : cout << "You entered X.\n"; // Result says "You entered I"
		break;

		default: cout << "You did not enter a number from 1 through 10.\n"; //Input Validatition
	}
	system ("pause");
	return 0;
}
Thanks! Problem solved.

I used "char" instead of "int" because it worked better. But after changing it back to INT AND surrounding my numbers with parenthesis instead of single quote marks, everything worked fine-even fixed my input validation.

Thanks a lot.
Note: I am learning on my own. This problem is from the book Starting out with C++ by Tony Gaddis. Chapter 4, problem #2.
No problem :) Me too, is still learning :))
But after changing it back to INT AND surrounding my numbers with parenthesis instead of single quote marks, everything worked

You don't need the parenthesis. e.g. case 1: would do just fine.
Last edited on
Topic archived. No new replies allowed.