Possible Duplicate:
How to convert integer value to Roman numeral string?
first time asking a question here. I have a project on the horizon I've been working a bit on, and can't seem to find anything like it asked here or elsewhere. The goal is to accept an integer with no upper constraint, and convert that into a roman numeral. Since integers do in fact have an upper boundary, I have to convert it into a string before parsing it, using apostrophes to denote each set of three characters placed into a sub-string. I'm having trouble conceptualizing the loops that a)assign roman numerals to what they see based on their location b)count each set of three by displaying apostrophes.
So far I have:
for(int i=0;i<(int)input.length()/3;i++){
temp=input.substr(i,3);
for(int j = 0; j < (int)temp.length(); j++){
if(j == 0){
if(temp[j] == '9') cout<<"CM";
else if(temp[j] >= '8') cout<<"DCCC";
else if(temp[j] >= '7') cout<<"DCC";
else if(temp[j] >= '6') cout<<"DC";
else if(temp[j] >= '5') cout<<"D";
else if(temp[j] == '4') cout<<"CD";
else if(temp[j] == '3') cout<<"CCC";
else if(temp[j] == '2') cout<<"CC";
else if(temp[j] == '1') cout<<"C";
}
else if(j == 1){
if(temp[j] == '9') cout<<"XC";
else if(temp[j] >= '8') cout<<"LXXX";
else if(temp[j] >= '7') cout<<"LXX";
else if(temp[j] >= '6') cout<<"LX";
else if(temp[j] >= '5') cout<<"L";
else if(temp[j] == '4') cout<<"XL";
else if(temp[j] == '3') cout<<"XXX";
else if(temp[j] == '2') cout<<"XX";
else if(temp[j] == '1') cout<<"X";
}
else if(j ==2){
if(temp[j] == '9') cout<<"IX";
else if(temp[j] == '8') cout<<"VIII";
else if(temp[j] == '7') cout<<"VII";
else if(temp[j] == '6') cout<<"VI";
else if(temp[j] >= '5') cout<<"V";
else if(temp[j] == '4') cout<<"IV";
else if(temp[j] == '3') cout<<"III";
else if(temp[j] == '2') cout<<"II";
else if(temp[j] == '1') cout<<"I";
}
}
}
The numerals display well enough on their own, but I'm having trouble figuring out how to tell the loop to start on the right, and work it's way left by threes, maintaining the actual place of the number in the input (e.g. 1234 should display 1 as I, not C. I also need to figure out the loop to write in the apostrophes.