Hi, so I know there's several forums covering roman numeral conversions out there but I'm in a intro c++ class and I have to write a code that does it with out any super fancy pre-written functions. (I got in trouble for that). So I have to do it the long, drawn out way. I'm quite on lost on how to do this and all my future labs depend on this working. So anyways, I have to use separate user-defined functions for each task in the code. Here's what I got so far.
#include <iostream>
#include <string>
usingnamespace std;
void roman_input( string &rom_in )
{
int n = 0;
while ( n == 0 )
{
cout << "Enter a roman numeral: ";
getline( cin, rom_in );
for ( int i = 0; i < rom_in.length(); i++ )
{
if ( rom_in.at(i) == 'I' )
n = 1;
elseif ( rom_in.at(i) == 'V' )
n = 1;
elseif ( rom_in.at(i) == 'X' )
n = 1;
elseif ( rom_in.at(i) == 'L' )
n = 1;
elseif ( rom_in.at(i) == 'C' )
n = 1;
elseif ( rom_in.at(i) == 'D' )
n = 1;
elseif ( rom_in.at(i) == 'M' )
n = 1;
else
{
n = 0;
cout << "Nope, try again!" << endl;
break;
}
}
}
string rom_to_dec( string dec_out )
{
for ( int i = 0; i < dec_out.length(); i++ )
{
if ( dec_out.at(i) == 'I' )
dec_out.at(i) = 1;
elseif ( dec_out.at(i) == 'V' )
dec_out.at(i) = 5;
elseif ( dec_out.at(i) == 'X' )
dec_out.at(i) = 10;
elseif ( dec_out.at(i) == 'L' )
dec_out.at(i) = 50;
elseif ( dec_out.at(i) == 'C' )
dec_out.at(i) = 100;
elseif ( dec_out.at(i) == 'D' )
dec_out.at(i) = 500;
elseif ( dec_out.at(i) == 'M' )
dec_out.at(i) = 1000;
}
return dec_out;
}
int main()
{
string input;
roman_input( input );
cout << input << endl;
rom_to_dec( input );
cout << input << endl;
return 0;
}
It produces the error if the roman numerals aren't uppercase or correct roman numerals and re-prompts for a correct roman numeral, and, it prints the roman numeral that is entered. The conversion part is not working though. Any help with this annoying code would be greatly appreciated.
So far I've only made it to the roman to decimal conversion. I suppose I'm really not sure wether its the conversion, string rom_to_dec( string dec_out ), or my function call in main that's not working. A lot of the examples for making this conversion certainly are much more involved than what I have at this point. I set myself back a few codes ago by using a pre-defined function to handle strings and now It's really hurting me not knowing how to write out code myself. So now I'm just extremely focused on trying to figure out how to write out such a code myself. I tried using arrays on one attempt and I got all kinds of errors about improper uses of pointers but I didn't even use pointers, that I know of anyway. I don't know, every day I'm a little more lost and clueless. I don't expect to get anyone to write out code for me, I just need direction/guidance on this stuff so I can maybe have a chance on getting through computer science. Thanks again for any and all assistance in this matter. :)