Classes help

closed account (jEb91hU5)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include <iostream>
#include <string>
#include <fstream>

using namespace std;

class romanNum
{
private:
	string roman;
	int integer;
	int piece;
public:
	void getNumbers(int integer)
	{
		const int SIZE = 20;

		romanNum record[SIZE];
		ifstream inputfile("numbers.txt");
		
		for (int i = 0; i < SIZE; i++)
		{
			inputfile >> record[i].integer;
			printRoman(integer);
		}
		
	}
	
	void printRoman(int integer)
	{
		if ((integer >= 4000) || (integer <= 0))
		{
			cout << endl << "Invalid Integer" << endl;
		}
		else
		{
			if (integer >= 1000)
			{
				piece = (integer / 1000);

				for (int i = 0; i < piece; i++)
				{
					roman += 'M';
				}
				integer %= 1000;
			}

			if (integer >= 100)
			{
				piece = (integer / 100);

				if (piece == 9)
				{
					roman += "CM";
				}
				else if (piece >= 5)
				{
					roman += 'D';

					for (int i = 0; i < piece - 5; i++)
					{
						roman += 'C';
					}
				}
				else if (piece == 4)
				{
					roman += "CD";
				}
				else if (piece >= 1)
				{
					for (int i = 0; i < piece; i++)
					{
						roman += 'C';
					}
				}
				integer %= 100;
			}

			if (integer >= 10)
			{
				piece = (integer / 10);

				if (piece == 9)
				{
					roman += "XC";
				}
				else if (piece >= 5)
				{
					roman += 'L';

					for (int i = 0; i < piece - 5; i++)
					{
						roman += 'X';
					}
				}
				else if (piece == 4)
				{
					roman += "XL";
				}
				else if (piece >= 1)
				{
					for (int i = 0; i < piece; i++)
					{
						roman += 'X';
					}
				}
				integer %= 10;
			}

			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';
					}
				}

			}

			cout << "Roman Numeral: " << roman << endl;
		}
	}
	
};
int main()
{
	romanNum romanObject;
	romanObject.getNumbers();

	system("pause");
	return 0;
}


Numbers in the txt file are as follows:
25
457
1465
13
85
238
3541
192
9
512
2841
36
935
372
1203
3783
96
271
603
2325
Last edited on
1
2
3
4
5
6
7
8
9
10
11
	void getNumbers( std::string filename )
	{
		ifstream inputfile( filename );
                if ( !inputfile ) return;
		
		int integer;
		while (inputfile >> integer)
		{
			printRoman( integer );
		}	
	}


1
2
3
4
5
6
7
8
int main()
{
	romanNum romanObject;
	romanObject.getNumbers( "numbers.txt" );

	system("pause");
	return 0;
}

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.
Last edited on
closed account (jEb91hU5)
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


Is there something wrong with the loop?
Last edited on
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 edited on
closed account (jEb91hU5)
Got it. Thank you so much!
closed account (jEb91hU5)
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.

And now you could use your romanNum like this:
1
2
3
romanNum romanObject;
cin >> romanObject;
cout << romanObject;

And of course the << and >> works also with filestreams :-)
> how do you print out the integers as well as the roman numerals?

You could add two methods to your class, e.g. 'int getInteger()' and 'std::string getRoman()'.

1
2
    int getInteger() const { return this->integer; }
    std::string getRoman() const { return this->roman; }


Use it like:
 
cout << "Integer: "<<romanObject.getInteger() << ", Roman Numeral: " << romanObject.getRoman() << endl;
Last edited on
closed account (jEb91hU5)
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:

1
2
3
4
5
6
7
8
9
10
11
 void getNumbers(string numbers)
	{
		ifstream inputfile("numbers.txt");

		int integer;
		while (inputfile >> integer)
		{
			cout << "Integer: " << integer << endl;
			printRoman(integer);
		}
	}


I just needed to think about it for a while and come back to it later.
Last edited on
Topic archived. No new replies allowed.