help me please

This program is supposed to make an integer greater than 0 and less than 4000 put into roman numerals. I'm not having any errors, but when I run the program and put in a 3300 it gives me something like MMMCCCXXXIII. Which is case 3, but in all of them rather than just the first two, which should give MMMCCC. Thanks for any help.

#include <iostream>

using namespace std;

int main()
{
int your_num;
int num = 0;

cout << "This program converts a positive integer up to 3,999 into the" << endl;
cout << "Roman number system."
" Enter a positive integer: ";
cin >> your_num;

cout << your_num << " written in roman numerals is ";

if ((your_num >= 4000) || (your_num <= 0))
{
cout << endl << "Your input must be a positive integer less than 4,000. Invalid input!";
return 1;
}

else if (your_num >= 1000)
{
num = (your_num / 1000);
your_num %= 1000;
}

switch (num)
{
case 1:
cout << "M";
break;
case 2:
cout << "MM";
break;
case 3:
cout << "MMM";
break;
}

if (your_num >= 100)
{
num = (your_num / 100);
your_num %= 100;
}

switch (num)
{
case 1:
cout << "C";
break;
case 2:
cout << "CC";
break;
case 3:
cout << "CCC";
break;
case 4:
cout << "CD";
break;
case 5:
cout << "D";
break;
case 6:
cout << "DC";
break;
case 7:
cout << "DCC";
break;
case 8:
cout << "DCCC";
break;
case 9:
cout << "CM";
break;
}

if (your_num >= 10)
{
num = (your_num / 10);
your_num %= 10;
}

switch (num)
{
case 1:
cout << "X";
break;
case 2:
cout << "XX";
break;
case 3:
cout << "XXX";
break;
case 4:
cout << "XL";
break;
case 5:
cout << "L";
break;
case 6:
cout << "LX";
break;
case 7:
cout << "LXX";
break;
case 8:
cout << "LXXX";
break;
case 9:
cout << "XC";
break;
}

if (your_num >= 1)
{
num = (your_num / 1);
your_num %= 10;
}

switch (num)
{
case 1:
cout << "I";
break;
case 2:
cout << "II";
break;
case 3:
cout << "III";
break;
case 4:
cout << "IV";
break;
case 5:
cout << "VI";
break;
case 6:
cout << "VI";
break;
case 7:
cout << "VII";
break;
case 8:
cout << "VIII";
break;
case 9:
cout << "IX";
break;
}
cout << "." << endl << endl;

system("pause");
return 0;
}

300
is >= 100,
num = 300/100 = 3
ynum = 300%100 = 0
switch, write ccc
if(..) fails, does not enter.
switch(num)… num is still 3 here

methinks everywhere in the code you need to say
if your_num ==0 num = 0
or wrap the switches inside the related if statements
Last edited on
Topic archived. No new replies allowed.