Basically, what I'm trying to do is convert decimals to roman numeral in both upper and lower case and then display it 19 other times, increasing by one each time it runs.
I have the numbers converting perfectly, but I cannot seem to be able to get the roman numerals to increment at all. Any help is appreciated.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main( )
{
string toLongRoman; //string for uppercase roman conversion
string toLower; //string for lowercaser roman conversion
int integer; //the user inputted variable
int piece; //how the number gets divided up into four parts for conversion
cout << "Enter a number in the range 1-5000: "; //lets user know what to input
cin >> integer; //where user inputs variable
int space = integer; //to save the variable for lowercase
int decimal = integer; //to save the variable to display original number
cout << "Decimal " << " " << " Long Roman " << " " << " Long Roman " << endl; //chart label
if ((integer >= 5000) || (integer <=0)) //sets limits on what numbers can be used
{
cout << endl << "The input value must be in the range 1 to 5000. Please try again." << endl;
} //error message
else
{
if (integer >= 1000) //for the thousands spot
{
piece = (integer/1000);
for (int i = 0; i < piece; i++)
{
toLongRoman += "M";
}
integer %=1000; //to divide up the number and leave a remainder
}
if (integer >= 100) //hundreds spot
{
piece = (integer/100);
for (int i = 0; i < piece - 5; i++)//for the remainder if greater than 5
{
toLongRoman += "C";
}
}
integer %= 100; //divide number up for the tens place
}
if (integer >= 10) //for tens place
{
piece = (integer/10);
if (piece < 5)
{
for (int i = 0; i < piece; i++)
{
toLongRoman += "X";
}
}
else if (piece >= 5) //display for 50
{
toLongRoman += "L";
for (int i = 0; i < piece - 5; i++) //properly displays number if greater than 50
{
toLongRoman += "X";
}
}
integer %= 10; //divides up number for ones place
}
if (integer >= 1) //shows single digits
{
piece = integer;
if (piece < 5)
{
toLongRoman += "I";
}
else if (piece >= 5) //diplays number properly if five or greater
{
toLongRoman += "V";
for (int i = 0; i < piece - 5; i++)
{
toLongRoman += "I";
}
}
}
}
if ((space >= 5000) || (space <=0)) //for lowercase numerals
{
cout << endl << "The input value must be in the range 1 to 5000. Please try again." << endl;
}
else
{
if (space >= 1000)
{
piece = (space/1000);
for (int i = 0; i < piece; i++)
{
toLower += "m";
}
space %=1000;
}
if (space >= 100)
{
piece = (space/100);
if (piece < 5)
{
toLower += "c";
}
else if(piece >= 5)
{
toLower += "d";
for (int i = 0; i < piece - 5; i++)
{
toLower += "c";
}
}
space %= 100;
}
if (space >= 10)
{
piece = (space/10);
if (piece < 5)
{
for (int i = 0; i < piece; i++)
{
toLower += "x";
}
}
else if (piece >= 5)
{
toLower += "l";
for (int i = 0; i < piece - 5; i++)
{
toLower += "x";
}
}
space %= 10;
}
if (space >= 1)
{
piece = space;
if (piece < 5)
{
toLower += "i";
}
else if (piece >= 5)
{
toLower += "v";
for (int i = 0; i < piece - 5; i++)
{
toLower += "i";
}
}
}
}
cout << decimal << " " << toLongRoman << " " << toLower << endl; //to display string values
for(int i = 1; i <= 19; i++) //for loop to iterate 19 more times
{
decimal++;
Again, what I need to do ishave the number increase by one, then convert that value to roman numeral. I need the number to refresh from being broken apart, if that makes since, to the original user inputted variable, then add that by one, then the integer+1 added by one again and so on.
Just a recommendation here - why don't you create a function to do the conversion work, which will make your code ALOT more reader friendly... for example...
1 2 3 4 5
string ConvertToRoman(int decValue)
{
//Do the work here
return aRomanValueString;
}
Once that's done, save the initial integer provided by the user and just increment it as needed, then pass the new value through the function to get the roman value... for example...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
int aDecimalValue;
string aRomanValue;
cout << "Enter a decimal number";
cin >> aDecimalValue;
//Increment by 1, 19 times
for (int i=0; i < 19; ++i)
{
++aDecimalValue;
aRomanValue = ConvertToRoman(aDecimalValue);
//Display or whatever you need to do next with the new roman value
}
}
By doing it this way, your "original number" is never changed (aDecimalValue) from the decimal format, but simply converted and saved into the variable "aRomanValue".