Roman Numerals

Write your question here.
I need some help with converting a user input (an integer) into roman numerals. Some of the numbers work but some do not work. for example 1900 shows a roman numeral that is mathematically correct but is not correct in terms of roman numerals. such as 4 is iv and not iiii.
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
  //benjamin Nguyne
//lab 8
#include <iostream>
using namespace std;

int main(){

int num;
char result;

do {

cout<<"enter a number:"<<endl;
cin>>num;

while (num>=1000){
cout<<"M";
num= num-1000;
}
while (num>=500){
cout<<"D";
num= num-500;
}
while (num>=100){
cout<<"C";
num= num-100;
}
while (num>=50){
cout<<"L";
num= num-50;
}
while (num>=10){
cout<<"X";
num=num-10;
}
while (num>=5){
cout<<"V";
num=num-5;
}
while (num>=1){
cout<<"I";
num=num-1;
}

cout<<endl;
cout<<"do you wish to continue? (y/n)"<<endl;
cin>>result;
}
while ((result=='y') || (result =='Y'));

return 0;
}
Last edited on
You put your code inside the code tags, not inside the tag itself. Otherwise, here:
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
#include <iostream>
using namespace std;

int main(){

int num;
char result;

do {

cout<<"enter a number:"<<endl;
cin>>num;

while (num>=1000){
cout<<"M";
num= num-1000;
}
while (num>=500){
cout<<"D";
num= num-500;
}
while (num>=100){
cout<<"C";
num= num-100;
}
while (num>=50){
cout<<"L";
num= num-50;
}
while (num>=10 || num == 9){
if(num >=10)
{
cout<<"X";
num=num-10;
}
else if (num == 9)
{
    cout << "IX";
    num-=9;
}
}
while (num>=5 || num == 4){
if(num >= 5){
cout<<"V";
num=num-5;
}
else if (num == 4)
{
    cout <<"IV";
    num-=4;
}
}
while (num>=1){
cout<<"I";
num=num-1;
}

cout<<endl;
cout<<"do you wish to continue? (y/n)"<<endl;
cin>>result;
}
while ((result=='y') || (result =='Y'));

return 0;
}
Last edited on
ok like that?
You need the tag on the end to say [/code]; you must have deleted that part accidentally.
ahhh ok sorry this is my first time posting thanks!
can anyone help me?
You need to learn to indent your code correctly, it makes it much easier to read.
thank you very much Jamerack, but could you explain 30 to 56 to me? i understand the first part but don't get line 30 on.
1
2
3
4
5
6
7
8
9
10
11
12
while (num>=10 || num == 9){
if(num >=10)
{
cout<<"X";
num=num-10;
}
else if (num == 9)
{
    cout << "IX";
    num-=9;
}
}

This is checking if the number is from 10-19, OR if it equals 9, if it equals 9, it just outputs a nine, and sets the number equal to 0. The other thing I changed does the same thing except for with 4.
Another thing, it is easier instead of doing something like num = num-5, it is easier to do num-=5. It accomplishes the same purpose, and there is no difference it is just a bit more compressed.
Last edited on
The thing is you don't have just the IV and IX variations for 4 and 9. It is also the case for 40(XL) and 90(XC) then 400(CD) and 900(CM).
Here my take on your problem:
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
#include <iostream>
#include <string>
using namespace std;

void printNumeral(string numeral, int noTimes = 1){
	for(int i = 0; i < noTimes; i++)
		cout << numeral;
}

int main(){

int num;
char result;


do {
int numeralM;
int numeralD;
int numeralX;

cout<<"enter a number:"<<endl;
cin>>num;

numeralM = num/1000;
printNumeral("M", numeralM);

num -= numeralM*1000;

numeralD = num/100;
if(numeralD == 9)
	printNumeral("CM");
else if(numeralD >= 5){
	printNumeral("D");
	printNumeral("C", numeralD - 5);
}
else if(numeralD == 4)
	printNumeral("CD");
else
	printNumeral("C", numeralD);

num -= numeralD*100;
numeralX = num/10;

if(numeralX == 9)
	printNumeral("XC");
else if(numeralX >= 5){
	printNumeral("L");
	printNumeral("X", numeralX - 5);
}
else if(numeralX == 4)
	printNumeral("XL");
else
	printNumeral("X", numeralX);

num -= numeralX*10;
// num is now < 9

if(num == 9)
	printNumeral("IX");
else if(num >= 5){
	printNumeral("V");
	printNumeral("I", num - 5);
}
else if(num == 4)
	printNumeral("IV");
else
	printNumeral("I", num);


cout<<endl;
cout<<"do you wish to continue? (y/n)"<<endl;
cin>>result;
}
while ((result=='y') || (result =='Y'));

return 0;
}

hey hiten, while your code works flawlessly, i do not know how the code works and have been trying to figure it out. some questions are what is the function of "void" in line 5 and the mathematical computation you did for the "else if" statements throughut lines 46, 50, 60, and 64. thanks
It just says that the function returns no value. If the initializer were "int," or "char," the function would return a value of the like type.

For the mathematical computations he used, refer to the printNumeral function at the beginning of the program.
@Hiten

I believe it is bad practice not to use the curly braces after the if, else if, and else conditionals.
Sorry for the delay. Here is the code again with some comments added.
@Jamerack, yeah... it's just a bad habit of mine...
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
#include <iostream>
#include <string>
using namespace std;

void printNumeral(string numeral, int noTimes = 1){ //this function takes a string for instance "X" or "CM" and prints it noTimes times
	for(int i = 0; i < noTimes; i++)	// say your number is 3000. Then printNumeral("M", 3) is the same as writing cout << "M" three
		cout << numeral;           //times
}				                     //if we call the function as printNumeral("M") then the second parameter is 1 by default
						   //it would be as if we'd call the function like printNumeral("M", 1);
int main(){

int num;
char result;


do {
int numeralM;
int numeralD;
int numeralX;

cout<<"enter a number:"<<endl;
cin>>num;

numeralM = num/1000;	//numeralM represents the thousands number eg: num = 3211 then numeralM is 3
printNumeral("M", numeralM);	//we print "M" numeralM times. In the above example we print MMM

num -= numeralM*1000;		//substracts from num the thousands part. If num = 3211 then after this, num is 211

numeralD = num/100;	//numeralD represends the hundreds number. In our case numeralD will be 2
if(numeralD == 9)	        //here are the conditions to avoid writing DCCCC (writing 4 times numeral C) or CCCC
	printNumeral("CM");	 //if the hundreds number is 9 then we need to print out CM
else if(numeralD >= 5){       //if not and if the number is greater than 5 (meaning it's 5,6,7 or 8) we need to print out a D(for 5)
	printNumeral("D");	//and as many C ( as many hundreds ) there are left that is numeralD-5
	printNumeral("C", numeralD - 5);
}
else if(numeralD == 4)    //if the hundreds number is less than 5 we need to test if it's 4 because in that case we need to avoid
	printNumeral("CD");	 //printing CCCC. If it's the case we print out CD
else
	printNumeral("C", numeralD);	//in all other cases we print C for numeralD times. In our example above numeralD is 2 so we print CC
											//
num -= numeralD*100;						//after this num is 11
numeralX = num/10;

if(numeralX == 9)							//same logic as above
	printNumeral("XC");
else if(numeralX >= 5){
	printNumeral("L");
	printNumeral("X", numeralX - 5);
}
else if(numeralX == 4)
	printNumeral("XL");
else
	printNumeral("X", numeralX);

num -= numeralX*10;
// num is now < 9

if(num == 9)
	printNumeral("IX");
else if(num >= 5){
	printNumeral("V");
	printNumeral("I", num - 5);
}
else if(num == 4)
	printNumeral("IV");
else
	printNumeral("I", num);


cout<<endl;
cout<<"do you wish to continue? (y/n)"<<endl;
cin>>result;
}
while ((result=='y') || (result =='Y'));

return 0;
}
I was bored
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
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
	const int ARRAY_SIZE = 13;
	map<int, string> roman;
	roman[1] = "I";		roman[90] = "XC";
	roman[4] = "IV";	roman[100] = "C";
	roman[5] = "V";		roman[400] = "CD";
	roman[9] = "IX";	roman[500] = "D";
	roman[10] = "X";	roman[900] = "CM";
	roman[40] = "XL";	roman[1000] = "M";
	roman[50] = "L";
	
	int arr[ARRAY_SIZE] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

	cout << "Please enter a number: " << flush;
	int num = 0;
	
	//validates user input
	while (!(cin >> num) || num <= 0)
	{
		cout << "Invalid input" << endl;
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "Please enter a number: " << flush;
	}
	cin.ignore(INT_MAX, '\n');

	//creates pointers to the beginning and end of arr
	const int *beg = arr;
	const int *end = arr + ARRAY_SIZE;


	//outputs roman numerals
	for (auto it = beg; it != end; ++it)
	{

		//if the array element pointed to by *it is less then or equal to 
		//num and num does not equal zero
		while (num != 0 && num / *it > 0)
		{
			num -= *it;
			cout << roman[*it]; //outputs the equivilent roman numeral value

		}
	}

	cin.ignore(INT_MAX, '\n');
	return 0;
}


[EDIT] fixed a minor bug
Last edited on
@Yanson

I quite like the way you did it, but I am still not advanced at C++, and I don't know what the flush command does. Could you explain it?
Last edited on
Flush clears the output buffer. So when we write statements such as
cout << "Please enter a number: "; We think of this string being printed to the screen. That's not actually true the string is sent to the ouput buffer where it waites to be output. By calling std::flush; we force the output buffer to print all character to our output ie. the screen , file, printer, ect.

So why is this important?
well in my program its not. The flush statement on line 20 and 29 could have been omitted. I do this mostly out of habbit. However, lets say you have a log file where you send error messages, and we try to print an error message without flushing the buffer. Then the computer crashes. The error message may not have been output to the log file, and that's the message we probably want.

its worth noting that endl; flushes the ouput buffer
Last edited on
Thank you!
Topic archived. No new replies allowed.