How to fix roman numeral program?

I wrote a program that converts decimals into roman numerals, but it's not working correctly. If you put in something like 42, it spits back XL which is 40. Anyone know how to fix it? I'm very new to programming too, so please explain if you know how to fix and how/why your fix works.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include<climits>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<string>

using namespace std;

string roman_from_int(short num);

int main(void)

{
    short num;
    char yes_no;

          
    cout << "\nWould you like to convert numbers into Roman Numerals?";
    cin >> yes_no;
    cin.ignore(INT_MAX, '\n');
    while ( toupper(yes_no) == 'Y' )
        
    {

        cout<< "\n\t Excelent! Let's get started!\n";
        
        cout<< "\n\t Please enter your number:\n";
        
        
        
        cin >> num;
        if ( num >= 4000 && num <= 0 )
        {
            cout << "\nError.  Please enter a number greater than 0 and less than 4000.\n";
        }
        else
        {
        
            string roman = roman_from_int(num);
            cout << "roman form is " << roman << ".\n";

        } 
            
            
            

        cin >> yes_no;
        cin.ignore(INT_MAX, '\n');
        
        
    }
    
        cout<<"\n\t\tThank you for using the Roman Numeral Converter!!!\n";
        
        cout << "\n\t\t\t\tHAVE A GREAT DAY!!!\n";
        return 0;
}

string roman_from_int(short num) 
{
    string romnum = "";
    {
    if(num >= 1000)
    {
        num = num - 1000;
        romnum = romnum + "M";
    }   
    else if (num >= 900)
    {
        num = num - 900;
        romnum = romnum + "CD";
    }
    else if (num >= 500)
    {
        num = num - 500;
        romnum = romnum + "D";
    }
    else if (num >= 400)
    {
        num = num - 400;
        romnum = romnum + "CD";
    }   
    else if (num >= 100)
    {
        num = num - 100;
        romnum = romnum + "C";
    }
    else if (num >= 90)
    {
        num = num - 90;
        romnum = romnum + "XC";
    } 
    else if (num >= 50)
    {
        num = num - 50;
        romnum = romnum + "L";
    }
    else if (num >= 40)
    {
        num = num - 40;
        romnum = romnum + "XL";
    }
    else if (num >= 10)
    {
        num = num - 10;
        romnum = romnum + "X";
    }
    
    else if (num >= 9)
    {
        num = num - 9;
        romnum = romnum + "IX";
    }
    else if (num >= 5)
    {
        num = num - 5;
        romnum = romnum + "V";
    }
    else if (num >= 4)
    {
        num = num - 4;
        romnum = romnum + "IV";
    }
    else if (num >= 1)
    {
        num = num - 1;
        romnum = romnum + "I";
    }
    }

    return romnum;
}
In a chain of if/else statements, only one of the compound statements is going to be executed. Get rid of the else's. Then you'll have another issue to fix, but you'll be closer to the solution. ;)
I think I'd have to use a while loop, but I'm not sure how to apply that to my program.
You're correct, you will need a while lloop (actually multiple) to handle repeated roman digits.
Consider a number such as 3832 (MMMDCCCXXXII).
Changing the if to a while is straight forward.
1
2
3
4
  while (num >= 1000)
  {  num = num - 1000;
      romnum = romnum + "M";
  }   


Here's a less tedious way to do the conversion:
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
string roman_digits (short & num, const string digits, int val)
{   string temp;

    while (num >= val) 
    {   temp += digits; //  Append roman digit(s)
        num -= val;
    }
    return temp;
}
        
string roman_from_int(short num) 
{   string romnum;
    
    romnum += roman_digits (num, "M", 1000);
    romnum += roman_digits (num, "CD", 900);
    romnum += roman_digits (num, "D", 500);
    romnum += roman_digits (num, "CD", 400);
    romnum += roman_digits (num, "C", 100);
    romnum += roman_digits (num, "XC", 90);
    romnum += roman_digits (num, "L", 50);
    romnum += roman_digits (num, "XL", 40);
    romnum += roman_digits (num, "X", 10);
    romnum += roman_digits (num, "IX", 9);
    romnum += roman_digits (num, "V", 5);
    romnum += roman_digits (num, "IV", 4);
    romnum += roman_digits (num, "I", 1);
    return romnum;
}

By multiple do you mean I'd need one for all of them? I got rid of all the else's and changed the first one to a while and it works for any # under 1000. Do I need to make all the ifs into whiles?
By multiple, I meant you need loops for any roman digit that can occur more than once (1000,100,10,1).

You don't need loops for roman digits that can occur only once (900,500,400,90,50,40,9,5,4).
Yeah. Just figured that out. Thanks for all the help!!!
Last edited on
Topic archived. No new replies allowed.