Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

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; }

OpenStudy (e.mccormick):

The problem is you are taking in just one character.

OpenStudy (anonymous):

So would I do int 1-5?

OpenStudy (anonymous):

int number?

OpenStudy (e.mccormick):

Int number, then change the cases too.

OpenStudy (e.mccormick):

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; } } ```

OpenStudy (e.mccormick):

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.

OpenStudy (anonymous):

Okay I see. How is case '1' different from case 1?

OpenStudy (e.mccormick):

'1' s a character. 1 is a number.

OpenStudy (e.mccormick):

Or more specifically, 1 is an integer, 1.0 is a float, etc.

OpenStudy (anonymous):

Alright. Thanks a lot!!

OpenStudy (e.mccormick):

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!

OpenStudy (e.mccormick):

strongly typed.... not s. hehe.

OpenStudy (anonymous):

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!