Integer To Roman Numeral Converter HElP

#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
using namespace std;

int main() {


bool game = true;
do {

char choice[25];


cout << " Would you like to use the Roman Numerator Calculator (y/n)?" << endl;

cin >> choice;

if (choice[0] == 'y') {

string roman = "";
int number;


cout << "Enter a number you want to convert" << endl;
cin >> number;

while (number >= 1000) {
roman += "M";
number -= 1000;
}

while (number >= 500) {
roman += "D";
number -= 500;
}

while (number >= 100) {

roman += "C";
number -= 100;
}

while (number >= 90) {
roman += "C";
number -= 90;

}

while (number >= 50) {
roman += "L";
number -= 50;
}

while (number >= 40) {
roman += "XL";
number -= 40;
}

while (number >= 10) {
roman += "X";
number -= 10;
}

while (number >= 9) {
roman += "IX";
number -= 9;
}

while (number >= 5) {
roman += "V";
number -= 5;
}

while (number >= 4) {
roman += "IV";
number -= 4;
}

while (number >= 1) {
roman += "I";
number -= 1;
}


cout << roman;
}


else if (choice[0] == 'n') {

cout << " Have a nice day" << endl;
game = false;
}

} while (game != false);
system("pause");
return 0;
}
Last edited on
Take a look at the Compound Assignment operators: http://www.cplusplus.com/doc/tutorial/operators/#compound

number -= 1000; is a shorthand way for writing number = number - 1000;. It reduces the value of the variable by the amount on the right. The same applies to roman += "M";, which appends the string "M" to the end of roman.
The idea of converting integer to roman number is easy.
There is only one rule: Biggest number comes first.

By knowing this, we could model the system like this:

M = 1000
CM = 900
D = 500
CD = 400
C = 100
XC = 90
L = 50
XL = 40
X = 10
IX = 9
V = 5
IV = 4
I = 1

So that by converting an integer to roman number, we search the biggest number the integer is greater than or equal to in the above list. Record the roman digits, and subtract the integer, and repeat above process until the integer reaches 0.

Thank You for the help.
Topic archived. No new replies allowed.