How can i get my program to just print out numbers 1 to 5? For example if I put in a 12 it will take the 1 as an output. Code in comments
int main() { char number; cout << "Enter a number from 1 to 5: "; cin >> number; switch (number) { case '1': cout << "The Roman numeral equivalent is: I" << endl; break; case '2': cout << "The Roman numeral equivalent is: II" << endl; break; case '3': cout << "The Roman numeral equivalent is: III" << endl; break; case '4': cout << "The Roman numeral equivalent is: IV" << endl; break; case '5': cout << "The Roman numeral equivalent is: V" << endl; break; default: cout << "The number must be in the range of 1 through 5 inclusive." << endl; }
The problem is you are taking in just one character.
So would I do int 1-5?
int number?
Int number, then change the cases too.
Also, if you want to pase code there are better ways. Pi a 1``` above and below so it formats it for being copied: ``` #include <iostream> using namespace std; int main() { int number; cout << "Enter a number from 1 to 5: "; cin >> number; switch (number) { case 1: cout << "The Roman numeral equivalent is: I" << endl; break; case 2: cout << "The Roman numeral equivalent is: II" << endl; break; case 3: cout << "The Roman numeral equivalent is: III" << endl; break; case 4: cout << "The Roman numeral equivalent is: IV" << endl; break; case 5: cout << "The Roman numeral equivalent is: V" << endl; break; default: cout << "The number must be in the range of 1 through 5 inclusive." << endl; } } ```
For larger blocks of code there are also services like http://pastebin.com/ and http://dpaste.com/ that can help keep messages from getting too long. They can also do code highlighting, which makes it more readable.
Okay I see. How is case '1' different from case 1?
'1' s a character. 1 is a number.
Or more specifically, 1 is an integer, 1.0 is a float, etc.
Alright. Thanks a lot!!
And in programming speak: Because C/C++ is strongly types you need to have a type agreement in the switch. If you try it with different ones, you will get some sort of type mismatch error. Have fun!
strongly typed.... not s. hehe.
u can still use a char and it would be the best choice because it takes less memory though this isnt necessary anymore. todays pcs have enough ram. just use "case 1:" instead of "case '1':". When u use '1' it equals 49 because the character '1' is at the 49th position in the ascii table.
Join our real-time social learning platform and learn together with your friends!