Convert Integers to Roman Numerals using Classes

Problem:

Write a program to read 20 numbers from a file and convert them into roman numerals. I need a class that will hold the numbers in private data and create public functions to allow the user to print in integer and roman numeral form.

What I'm having trouble with is setting up constructors for my classes. I'll show what I have so far.

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
#include<iostream>
#include<fstream>

using namespace std;

class romanConvert
{
private:
	int numbers[20];
	string roman;
public:
	void setNum();
	void setRoman();
};

void romanConvert::setNum()
{
	int numbers[20];

	ifstream infile;
	infile.open("numbers.txt");

	for (int i = 0; i < 20; i++)
	{
		infile >> numbers[i];
	}

}

void romanConvert::setRoman()
{
	
}

void main()
{
	cout << "The numbers in integer format are: " << endl;
	cout << "The numbers in roman numeral format are: " << endl;
	system("pause");

}
void main is something that works but is illegal. Its supported on less strict compiler settings.

why do you have an array of 20 numbers? Should number just be an integer, probably unsigned? I don't get it.

romanConvert() {} //a default constructor. you have this already (compiler makes it), but its hidden.

romanConvert(string s) {roman = s;} //something like this? ideally it would check that s is a legal roman string … maybe romanconvert can be made to return a 'invalid' result and be used here...




Last edited on
I put the array because I was having trouble figuring out how to input the numbers from my input file into int numbers in the class. I couldn't think of another way to do it.
ah. it looks like your class is supposed to be a single value --- akin to 'int'.
if that is the case, make a container of your class in main. OR, if you want to go with the class being a container itself, you need an array of strings to go with the array of ints, google 'parallel arrays'. but I recommend that you make the class a single value and contain it another way (this is just a better design, you will get to this later, I don't know if i can explain it fully here).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vector<romanConvert> filevalues; 
int tmp;
romanConvert rc;

while( filevar >> tmp)
{
    rc.setnum(tmp);
    filevalues.push_back(rc);    
}

or if vectors are above you still,
romanConvert rc[20];
for(i = 0; i < 20; i++)
{
filevar >> tmp;
rc[i].setnum(tmp);
}

Topic archived. No new replies allowed.