Originally Posted by
INeedSleep
i think i pretty much finished these levels:
Add (Level 3) to use functions to break your program into more manageable pieces. I'd recommend the bounds checking code for the user's number and the display of the result. In fact, that results display itself could be broken down into a couple of functions: random pre-message and random post-message (with the result itself printed between these calls).
You can add another (Level 1.5) to place the repetitious pattern of converting [each digit of] the user's number into A re-usable function.
i have to look over the code more throughly
Code:
//updated 11/7/07
// Jason helped: Wed Nov 7 15:49:56 CST 2007
#include <cctype>
#include <string>
#include <iostream>
#include <climits>
using namespace std;
// single digit n, three char string with ones digit in position 0,
// fives digit in position 1, and tens digit in position 2. returns
// digit n represented in Roman numeral form.
string to_roman(short n, const string & OneFiveTen);
int main()
{
char ans='y';
string roman;
short input; //user input
//string names converted to roman numerals
const string rome1="I";
const string rome5="V";
const string rome10="X";
const string rome50="L";
const string rome100="C";
const string rome500="D";
const string rome1000="M";
while(tolower(ans)=='y')
{
cout<<"what number would you like to convert?:\n";
cin>>input;
cin.ignore(INT_MAX, '\n');
if(input>0 && input<4000 )
{
roman="";
//thousands place to get remainder(1000-3000)
roman = roman + to_roman(input/1000, rome1000);
//hundreds place to get remainder(100-300)
roman = roman + to_roman(input%1000/100, rome100+rome500+rome1000);
//tens place
roman = roman + to_roman(input%100/10, rome10+rome50+rome100);
//ones place
roman = roman + to_roman(input%10, rome1+rome5+rome10);
cout << "that is converted from a" <<" " << input << " "
<<"into" << " " << roman <<" " << "in roman numerals\n";
cout << "would you like to try again?";
cin >> ans;
}
else
{
ans='n';
cout<<"then get the f*&k out then !";
}
if(ans =='y' || ans =='Y')
{
cout<<"dont come the f@*k back !";
}
}
return 0;
}
// single digit n, three char string with ones digit in position 0,
// fives digit in position 1, and tens digit in position 2. returns
// digit n represented in Roman numeral form.
string to_roman(short n, const string & OneFiveTen)
{
string in_roman;
short i;
if( n == 4 || n == 9) // handle 4 and 9 separately
{
in_roman=OneFiveTen[0];
if(n==4)
{
in_roman+=OneFiveTen[1];
}
else // if(n==9)
{
in_roman+=OneFiveTen[2];
}
}
else
{
if(n>=5) // handle 5 ..and start 6,7,8
{
in_roman+=OneFiveTen[1];
n=n-5;
}
i=1;
while(i<=n)
{
in_roman+=OneFiveTen[0];
i=i+1;
}
}
return in_roman;
}
ignore the "then get the f*&k out then !" and "dont come the f@*k back !"
Jason is my professor, btw