Roman Numbers/Switch Statement

int main(int argc, const char * argv[])
{
char choice;

// Roman numerals 1-10
// Use switch statement. Use input validation for 1-10.
cout << "Enter a number from 1-10: " << endl;
cin >> choice;

switch(choice)
{
case '1': cout << "The roman number equivilant is: I. \n";
break;
case '2': cout << "The roman number equivilant is: II. \n";
break;
case '3': cout << "The roman number equivilant is: III. \n";
break;
case '4': cout << "The roman number equivilant is: IV. \n";
break;
case '5': cout << "The roman number equivilant is: V. \n";
break;
case '6': cout << "The roman number equivilant is: VI. \n";
break;
case '7': cout << "The roman number equivilant is: VII. \n";
break;
case '8': cout << "The roman number equivilant is: VIII. \n";
break;
case '9': cout << "The roman number equivilant is: IX. \n";
break;
case '10': cout << "The roman number equivilant is: X. \n";
break;
default: cout << "You did not enter a number from 1-10. \n";
}
return 0;
}
// Problem: If I enter 10, it gives me the CASE 1 answer. If I enter 44, it gives me the CASE 4 answer instead of DEFAULT. I also get a warning on CASE 10 for "multi characters".
(From "Starting Out C++..." by Tony Gaddis 8ed. Chapter 4, problem 2)
Last edited on
a Char variable can only accept one character value. so when you enter "10" it's not going to accept the 0 and will only accept the "1" which is why when you enter "44" it just grabbed the first "4" and threw away the other "4".
I see that you're working only with numbers, so why don't you declare choice as an int value? then just get rid of those single quotes

Last edited on
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
int main(int argc, const char * argv[])
{
int choice;

// Roman numerals 1-10
// Use switch statement. Use input validation for 1-10.
cout << "Enter a number from 1-10: " << endl;
cin >> choice;

switch(choice)
{
case 1: cout << "The roman number equivilant is: I. \n";
break;
case 2: cout << "The roman number equivilant is: II. \n";
break;
case 3: cout << "The roman number equivilant is: III. \n";
break;
case 4: cout << "The roman number equivilant is: IV. \n";
break;
case 5: cout << "The roman number equivilant is: V. \n";
break;
case 6: cout << "The roman number equivilant is: VI. \n";
break;
case 7: cout << "The roman number equivilant is: VII. \n";
break;
case 8: cout << "The roman number equivilant is: VIII. \n";
break;
case 9: cout << "The roman number equivilant is: IX. \n";
break;
case 10: cout << "The roman number equivilant is: X. \n";
break;
default: cout << "You did not enter a number from 1-10. \n"; 
}
return 0;
}


Try this it will work u declared choice as char so it ll consider only first value
It doesn't work. I changed char to int.
The single quotes were taken out too.
Everything works except the case 10 and another number other than 1-9

case 10: cout << "The roman number equivilant is: X. \n";
break;

Last edited on
Topic archived. No new replies allowed.