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.
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?
//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".
#include <iostream>
#include <string>
usingnamespace 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";
}
elseif (piece >= 5) {
roman += 'L';
for ( int i = 0; i < piece - 5; i++) {
roman += 'X';
}
}
elseif (piece == 4) {
roman += "XL";
}
elseif (piece >= 1) {
for (int i = 0; i < piece; i++) {
roman += 'X';
}
}
}
if (integer >= 1)
{
piece = integer;
if(piece == 9)
{
roman += "IX";
}
elseif (piece >= 5)
{
roman += "V";
for (int i = 0; i < piece - 5; i++)
{
roman += 'I';
}
}
elseif (piece == 4)
{
roman += "IV";
}
elseif (piece >= 1)
{
for (int i = 0; i < piece; i++)
{
roman += 'I';
}
}
}
}
system ("pause");
return 0;
}
#include <iostream>
#include <string>
usingnamespace 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.
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.
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.
#include <iostream>
#include <string>
usingnamespace 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.
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.