Roman Numeral output Program -- Need Hint

Been asked to create a Roman numeral output program which lists the numbers on one side and the roman numerals next to it from 1-100 (example below).

I am struggling to link the numbers from the left to the roman numeral string of characters and am not sure what to look for to do some research on this.

I was thinking of generating the 1-100 with a while (i < 100) then link i to the roman numerals but not sure if this is the right way to go about this.Any hints or tips would be great.

1 I
2 II
3 III
4 IV
5 V
Well you may want to look for a pattern for the Roman Numerals.

1
2
3
4
5
6
7
I =    1
V =    5
X =   10
L =   50
C =  100
D =  500
M = 1000


Also note how many times the individual elements repeat.

This chart may help you visualize the problem: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
And this may help explain how to use Roman Numbers: http://en.wikipedia.org/wiki/Roman_numerals and http://www.novaroma.org/via_romana/numbers.html

Last edited on
Thanks for your fast reply, i understand how the roman numeral system works and am able to program so the user can input a number and convert it to a roman numeral.

I'm struggling to be able to change it from a user input to a table display such as the example you gave me. I can't work out how to connect the list of numbers to display the roman numerals is the problem.
So show what you've tried. You say you can convert a user inputed number to a Roman Numeral so I don't really understand what you are trying to do. If you want a table why not use a loop?

closed account (3qX21hU5)
Like jlb said give us what you have so far and we can help guide you in the right direction.

Also probably just a type but wanted to point it out incase it wasn't.

I was thinking of generating the 1-100 with a while (i < 100)
while (i < 100) would only print to 99. If you want to print 1-100 I would do something like this for (int i = 1; i <= 100; ++i).
Last edited on
Ok this is what i have so far.

#include <iostream>
#include <string>

using namespace std;

int main ()
{

//Initialize Variables

int integer = 0;
int piece;
string roman;


//The For loop.
for (int i = 0; i <= 10; i++)
{
cout << integer << "\t\t" << roman << endl;
integer++;

//If statments
//to work out what char is needed for the integer
if (integer == 10)
{
roman += "X";
}

if (integer >= 1)
{
piece = integer;

if(piece == 9)
{
roman += "IX";
}
else if (piece >= 5)
{
roman += "V";

for (int i = 0; i < piece - 5; i++)
{
roman += 'I';
}
}
else if (piece == 4)
{
roman += "IV";
}
else if (piece >= 1)
{
for (int i = 0; i < piece; i++)
{
roman += 'I';
}
}
}


}

system ("pause");

return 0;

}



However this give the result of

0
1 I
2 III
3 IIIIII
4 IIIIIIIV
5 IIIIIIIVV

so it is carrying the previous value which i guess is because i use the variable "integer".
Please use code tags when posting code. You need to clear() your string after you print it.

1
2
      cout << integer << "\t\t" << roman << endl;
      roman.clear();


Also note your loop should start a 1 not zero, remember there is no zero in Roman Numerals.
Last edited on
Thank you very much for your help, hopefully i will be able to finish it now. Also I'm new to posting so sorry for not using code tags.
Have got closer to finishing the project but hit another problem i can get the correct roman numerals for the 10ths so:

10 X
20 XX
30 XXX

However the code is adding:

10 XVIIIII
20 XXVIIIIIIIIII
30 XXXVIIIIIIIIIIIIIII

I think this of this code that causes this issue.

1
2
3
4
5
6
7
8
9
else if (piece >= 5)
		{
			roman += "V";

			for (int i = 0; i < piece - 5; i++)
			{
				roman += 'I';
			}


but not sure how to fix it i think i might need to add

 
integer %= 10;


but this changes my loop, and loops 0-9 over and over. This is the full code:

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
#include <iostream>
#include <string>

using namespace std;

int main ()
{

	//Initialize Variables

	int integer = 0;
	int piece;
	string roman;


	//The For loop.
	for (int i = 0; i <= 100; i++)
	{
		cout << integer << "\t\t" << roman << endl;
		integer++;
		roman.clear();

	//If statments 
	//to work out what char is needed for the integer
	if (integer >= 10){
		piece = (integer / 10);

		if (piece >= 9) {
			roman += "XC";
		}
		else if (piece >= 5) {
			roman += 'L';

			for ( int i = 0; i < piece - 5; i++) {
				roman += 'X';
			}
		}
		else if (piece == 4) {
			roman += "XL";
		}
		else if (piece >= 1) {

			for (int i = 0; i < piece; i++) {
				roman += 'X';
			}
		}
		
	}

	if (integer >= 1)
	{
		piece = integer;

		if(piece == 9)
		{
			roman += "IX";
		}
		else if (piece >= 5)
		{
			roman += "V";

			for (int i = 0; i < piece - 5; i++)
			{
				roman += 'I';
			}
		}
		else if (piece == 4)
		{
			roman += "IV";
		}
		else if (piece >= 1) 
		{
			for (int i = 0; i < piece; i++)
			{
				roman += 'I';
			}
		}
	}

	
	}

	system ("pause");

	return 0;

}


Any ideas how to fix would be great. Thanks.
closed account (3qX21hU5)
I think you are going about the design all wrong personally.

Lets take the number 27 for example, 27 in Roman would be XXVII correct?

So the way we go about this is reducing the number like this.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string romanNumeral;
    int number;

    cout << "Enter a number to convert: ";
    cin >> number;

    while (number != 0)
    {
        if (number >= 10)
        {
            romanNumeral += "X";    // Add the X to the string for the 10 we minused.
            number -= 10;           // We minus 10 because that is what X equals
            continue;               // We use continue to start again at the top after we find the bigest match
        }

        if (number >= 5)
        {
            romanNumeral += "V";
            number -= 5;
            continue;
        }

        if (number >= 1)
        {
            romanNumeral += "I";
            number -= 1;
            continue;
        }
    }

    cout << romanNumeral;
}


Now this is not a fully functional program, you will need to change this so that it support every number up to and including 100. You will also have to change a few more things but you can figure them out :).

If you know function it would also be better to make a function for each Roman Numeral (X, V, I, ect) instead of using if statement.

Hope this helps a bit.
Last edited on
Yes i would love to have this as my design and have the user input a number and convert it to a roman numeral, however the task given is to output to screen 1-100 in roman numerals one number per line. So i think the design i am going for is correct however I will try to confirm this. But thanks for your help I do appreciate you trying to help.
closed account (3qX21hU5)
Just because my example had the user enter a number doesn't mean it couldn't be used to print out the numbers 1-100...

The whole point is that you are making the problem more complicated then it has to be. My example was mean to point out the if statements and using them to convert a number to roman numeral. The user input had nothing to do with it.

Here is another example that shows that it can do what your assignement asks for.

I purposely left some features out so that you can figure out how to do it by yourself.

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string romanNumeral;
    int number;


    for (int i = 1; i != 51; ++i)
    {
        number = i;
        while (number != 0)
        {
            if (number >= 10)
            {
                romanNumeral += "X";    // Add the X to the string for the 10 we minused.
                number -= 10;           // We minus 10 because that is what X equals
                continue;               // We use continue to start again at the top after we find the bigest match
            }

            if (number >= 5)
            {
                romanNumeral += "V";
                number -= 5;
                continue;
            }

            if (number >= 1)
            {
                romanNumeral += "I";
                number -= 1;
                continue;
            }
        }
        cout << romanNumeral << endl;
        romanNumeral = "";
    }

}


But anyways this is just a suggestion, and you can continue to do it your way if that it is the correct way to do it.
Sorry I didn't understand that you meant that i should try working out the numerals differently. I will give this a go. Thank you again.
I have just tried it the way you have suggested and it works very well, i can see my way was a lot more complex and would just like to thank you again for you time and assistance.
Last edited on
Topic archived. No new replies allowed.