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)
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".
int main(int argc, constchar * 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