Hello. I saw a post on the same assignment earlier, but was hoping to get help on my current code as well. The problem is to convert 20 integers read in from a file to Roman Numerals. This is what I have so far; the code to convert to Roman Numerals works perfectly fine. I just need some help integrating classes into my code. I know my "main" is incorrect.
I don't know what you expect from your program, but writing a class for it is not really needed because you could have instead two freestanding functions.
Using a class is necessary for the assignment. I changed the code to
1 2 3 4 5 6 7 8 9 10
void getNumbers(string numbers)
{
ifstream inputfile("numbers.txt");
int integer;
while (inputfile >> integer)
{
printRoman(integer);
}
}
and
1 2 3 4 5 6 7 8
int main()
{
romanNum romanObject;
romanObject.getNumbers("numbers.txt");
system("pause");
return 0;
}
It now compiles without errors, but the roman numerals are not correct. They seem to just build on each other.
Roman Numeral: XXV
Roman Numeral: XXVCDLVII
Roman Numeral: XXVCDLVIIMCDLXV
Roman Numeral: XXVCDLVIIMCDLXVXIII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXV
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLI
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIX
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLI
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVI
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXV
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIIIMMMDCCLXXXIII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIIIMMMDCCLXXXIIIXCVI
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIIIMMMDCCLXXXIIIXCVICCLXXI
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIIIMMMDCCLXXXIIIXCVICCLXXIDCIII
Roman Numeral: XXVCDLVIIMCDLXVXIIILXXXVCCXXXVIIIMMMDXLICXCIIIXDXIIMMDCCCXLIXXXVICMXXXVCCCLXXIIMCCIIIMMMDCCLXXXIIIXCVICCLXXIDCIIIMMCCCXXV
It seems that it's all working correctly. The only issue is that it's outputting the previous roman numeral that it found along with the new one.
Your output:
Roman Numeral: XXV
Roman Numeral: XXVCDLVII
should be:
Roman Numeral: XXV
Roman Numeral: CDLVII
^You have an extra XXV from the previous roman numeral you found. You have to remove the previous input from your "roman" string. If you need to have all the outputs in the string for some reason, then you need to keep track of where one input ends and another begins so that you can output correctly.
With your roman string, you keep "+=" it, meaning nothing inside the string is being replaced, it's just being added onto.
Simply adding:
roman = "";
To the top of your printRoman function will fix this.
Last question for this code--which I don't feel great about not being able to figure it out--how do you print out the integers as well as the roman numerals?
Something like this:
cout << "Integer: " << (what goes here?) << ", Roman Numeral: " << roman << endl;
@JLaw21 but is this really what you want? I guess you want to write a class which represents a roman number.
If it this what you want, you need to have at your roman class a method (function) which reads a number and converts it to roman representation (you have it nearly). Further, it would be nice if could overload std::istream and std::ostream for reading/writing a number into/out to your roman class.
Here I show you, how you overload std::istream and std::ostream for using the >> and << for your roman class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
std::ostream & operator<<( std::ostream & os, const romanNum & rom )
{
os << rom.roman;
return os;
}
std::istream & operator>>( std::istream & is, romanNum & rom) // no const
{
int integer;
is >> integer;
// You have to write this method still, basing on your 'printRoman()'
rom.getNumber( integer );
return is;
}
All to do is, adding friend std::ostream & operator<<( std::ostream &, const romanNum &);
into the body of your class.
Thanks for the input. I'm not quite advanced enough to understand that. But I did figure out a simple way to list them above their Roman numeral counterpart in the output like so: