Roman Numerals

I am working on an arabic to roman numeral converter. The following code works, if the user doesn't enter any zeroes and only 1 or less digits are greater than 4 in value. I realize this code is kind of convoluted, so I'm about ready to take what I have as a lesson and start over. Problem is, I'm not sure what will work better. Maybe ya'll can criticize the hell out of this code to help me make some new ideas. Thanks...
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<cstring>
#include<iostream>

using namespace std;

string roman(string arabic){
     int low=0,high=0,one=0,five=0,ten=0,fifty=0,hundred=0,fh=0,thou=0;  //variables for digit counts
     string tempstr;
     int x;
     
     char tempc;
     for(int t=arabic.length()-1;t>=0;--t){  //start at ones place and process backwards
             tempc=arabic[t];  //get current character
             high=low=0;    //reset counters
             
             switch(tempc){      //process current letter
                           case '1':
                                low+=1;
                                break;
                           case '2':
                                low+=2;
                                break;
                           case '3':
                                low+=3;
                                break;
                           case '4':
                                low+=4;
                                break;
                           case '5':
                                high+=1;
                                break;
                           case '6':
                                high+=1;
                                low+=1;
                                break;
                           case '7':
                                high+=1;
                                low+=2;
                                break;
                           case '8':
                                high+=1;
                                low+=3;
                                break;
                           case '9':
                                high+=1;
                                low+=4;
                                break;
                                }
             x=arabic.length()-t-1;  //find out which digit was just processed
                                
             switch(x){    //process info from last switch
                       
                                       case 0:
                                            one+=low;
                                            five+=high;
                                            break;
                                       case 1:
                                            ten+=low;
                                            fifty+=high;
                                            break;
                                       case 2:
                                            hundred+=low;
                                            fh+=high;
                                            break;
                                       case 3:
                                            thou+=low;
                                            thou+=high;
                                            break;
                                            }
                                            }
                                            
             tempstr="";              //process string to return
             for(int t=0;t<thou;++t){   //process thousands
                     tempstr+="M";
                     }
             if (fh==4 && hundred==4){            //process hundreds
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='D';
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='C';
                       }
             else if(fh==4){
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='D';
                       for(int t=0;t<hundred;++t){
                               tempstr+='C';
                               }
                       }
             else if(hundred==4){
                  for(int t=0;t<fh;++t){
                          tempstr+='D';
                          }
                  tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='C';
                          }
             else {
                  for(int t=0;t<fh;++t){
                          tempstr+='D';
                          }
                  for(int t=0;t<hundred;++t){
                          tempstr+='C';
                          }
                          }
                          //process tens
             if (fifty==4 && ten==4){            
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='L';
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='X';
                       }
             else if(fifty==4){
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='L';
                       for(int t=0;t<ten;++t){
                               tempstr+='X';
                               }
                       }
             else if(ten==4){
                  for(int t=0;t<fifty;++t){
                          tempstr+='L';
                          }
                  tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='X';
                          }
             else {
                  for(int t=0;t<fifty;++t){
                          tempstr+='L';
                          }
                  for(int t=0;t<ten;++t){
                          tempstr+='X';
                          }
                          }
                          //process units
                          if (five==4 && one==4){            
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='V';
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='I';
                       }
             else if(fh==4){
                       tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='V';
                       for(int t=0;t<hundred;++t){
                               tempstr+='I';
                               }
                       }
             else if(hundred==4){
                  for(int t=0;t<fh;++t){
                          tempstr+='V';
                          }
                  tempstr.resize(tempstr.length()+1);
                       tempstr[tempstr.length()-1]=tempstr[tempstr.length()-2];
                       tempstr[tempstr.length()-2]='I';
                          }
             else {
                  for(int t=0;t<fh;++t){
                          tempstr+='V';
                          }
                  for(int t=0;t<hundred;++t){
                          tempstr+='I';
                          }
                          }
             return tempstr;
             }
             

int main(){
    cout<<"\nRoman Numeral Converter\nBy:Joshua Smith";
    string str;
    int ans=1;
    while(ans==1){
                  cout<<"\nPlease eneter a string of Arabic Numbers with value less than or equal to 3000: ";
                  cin>>str;
                  cout<<"\nI will now compute the Roman Numeral Equivalent...";
                  str=roman(str);
                  cout<<endl<<str;
                  cout<<"\nAnother conversion (1=yes, other =no)";
                  cin>>ans;
                  }
                  return 0;
                  }
                  
This won't fix it all, but in your case 3 you have:
1
2
thou+=low;
thou+=high;

Wouldn't you want them to be stored in two variables?
well, this is just a shortcut. There technically is no roman numeral for more than three thousand, because there is no digit for five thousand. Yes I know in modern roman numerals you can use a V with a line over it, but that's outside the scope of this program. Roman Numerals are basically in base 5, without a zeroes placeholder, so for each digit I count the low part and the high part then redirect it to the proper character. For thousands, I gave it the low and high parts. This will give you MMMMMMMMI for 8001, which is wrong, but the program states this anyway.
But you're putting both low and high into thou.
actually I had a lot of errors where I copied and pasted the different if blocks. It actually works now, but it still seems like there should have been an easier method.
Topic archived. No new replies allowed.