Problem with classes and constructor

I'm trying to write a program that will use a class convert Roman numerals to a decimal integer, but I can't figure out how to get my final number from the constructor to main.
I can't seem to figure out how to get the information from the constructor,
but if i test it with cout the integer value has been multiplied 7 times.
I'm not allowed to have any input or output in the class itself, only in main.
Any insight would be greatly appreciated.

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


using namespace std;

const int index = 20;
const int romInd = 7;

//class for converting the numerals
class romanConvert
{
public:

	char romLet[romInd] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

	int getRoman(int decInteger)
	{
		return decInteger;
	}

	void setRoman(char romLet)
	{
		romLet = romNum;
	}
	
	romanConvert(char romNum[index],int decInteger);

private:
	char romNum;
	int decInteger;
};

int main()
{
	//index to hold the user entered values
	char numeral[index];
	int decInteger = 0;


	//explains the program to user
		cout << "This program will take a Roman numeral then, " << endl
			 << "return it's value as a decimal integer." << endl << endl;
		cout << "The value of Roman numerals:" << endl;
		cout << "M = 1000" << endl
			 << "D =  500" << endl
			 << "C =  100" << endl
			 << "L =  50" << endl
			 << "X =  10" << endl
			 << "V =   5" << endl
			 << "I =   1" << endl << endl;

		//user input prompt
		cout << "To exit, type E as your first entry." << endl;

		cin >> numeral;

			//loops to read each character input individually
			for (int i = 0; i < index; i++)
			{
				//ensures all input is upper case when read
				numeral[i] = toupper(numeral[i]);

				//gives the user the option to exit early
				if (numeral[0] == 'E')
				{
					cout << "Exiting program." << endl;

					//don't forget to delete this
					cin.ignore(2);
					return '\0';
				}

			}

		//setting constructer as the user input values
		romanConvert converter(numeral, decInteger);

		//constructor call
		converter;

		converter.getRoman();

	//don't forget to delete this
	cin.ignore(2);

	return 0;
}

//constructor definition
romanConvert::romanConvert(char romNum[index], int decInteger)
{
	int romVal[romInd] = { 1000, 500, 100, 50, 10, 5, 1 };
	char romLet[romInd] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

	int romLen = 0, check[index] = { 0 };

	bool good = false;

	//used to populate romNum
	for (int i = 0; i < index; i++)
	{
		if (romNum[i] != '\0')
			romLen++;
		else
			i = index;
	}

	//finds the number of characters input by the user
	for (int i = 0; i < romLen; i++)
	{
		good = false;


		//converts the roman number to an integer
		for (int x = 0; x < romInd; x++)
		{
			if (romNum[index] == romLet[i])
			{
				good = true;
			}

			for (int i = 0; i < romLen; i++)
			{

				for (int x = 0; x < romInd; x++)
				{
					if (romNum[i] == romLet[x])
					{
						check[i] = romVal[x];
					}
				}
			}

			for (int i = 0; i < romLen; i++)
			{
				if ((check[i + 1] > check[i]))
				{
					decInteger += (check[i + 1] - check[i]);
					i++;
				}
				else
					decInteger += check[i];
			}
		}
	}
	cout << "Test output: " << decInteger << endl;
}
1
2
	//constructor call
		converter;

That makes no sense.

A constructor is a function that runs when an object is created.
romanConvert converter(numeral, decInteger); This line of code calls the constructor.


You're thinking about it wrong. If the information is stored in the object, you need that object to provide a function that gives you that information.

In the constructor, you are setting class member variables. Which of the class member variables do you want in main? Is it decInteger? You've already written a function for returning that.

But your constructor is handed values to use as decInteger? It looks like you don't understand the idea of an object, and you think that a class is just something for organising functions.


Last edited on
Thanks, for taking the time to respond to my question.

Admittedly, I am pretty lost here. Classes are something I've never used before and my book's section on constructors didn't make very much sense to me.

So, you're saying that the constructor is just there to set the variables that will be used with the functions inside the class?

That would mean that I need to move the code I have inside of the constructor definition to a a function inside of the class and use it to convert the numbers, then pass the value to the function returning decInteger, right? Or is that still on the wrong track?
Constructor is a function that is run when the object is created. You can make that function do whatever you like. Commonly, it is used to ensure that the object is in a good state and ready to use. Sometimes the constructor does nothing. Sometimes it sets values. Sometimes it acquires resources that the object will need. Whatever you like towards the goal of making the object ready for use.

Given your class, I think it should have a function that receives the number from the user input, and processes it to set decInteger. Another function in the class to return that decInteger values.

This would make your class useful; every time you set the number, the decInteger is recalculated.
I've tried to change it based on your suggestions, and it compiles without errors, but the romanArray function I'm trying to use to convert it, doesn't alter the value of decInteger, and I can't figure out why.


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
154
155
156
157
158
159

#include<iostream>


using namespace std;

//global ints to use as array indices
const int index = 20;
const int romInd = 7;

//class for converting the numerals
//returns the value of decInteger, but it doesn't alter the value
class romanConvert
{
public:
	int romVal[romInd] = { 1000, 500, 100, 50, 10, 5, 1 };
	char romLet[romInd] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };

	int decInteger = 0;

	int romLen = 0, check[index] = { 0 };

	bool good = false;

	romanConvert (char romNum[index]);

	char romanArray(char romNum[index])
	{
		//used to populate romNum
		for (int i = 0; i < index; i++)
		{
			if (romNum[i] != '\0')
				romLen++;
			else
				i = index;
		}

		//finds the number of characters input by the user
		for (int i = 0; i < romLen; i++)
		{
			good = false;


			//converts the roman number to an integer
			for (int x = 0; x < romInd; x++)
			{
				if (romNum[index] == romLet[i])
				{
					good = true;
				}
			}
				for (int i = 0; i < romLen; i++)
				{

					for (int x = 0; x < romInd; x++)
					{
						if (romNum[i] == romLet[x])
						{
							check[i] = romVal[x];
						}
					}
				}

              //alters the actual value of decInteger based on the information from check
				for (int i = 0; i < romLen; i++)
				{
					if ((check[i] < check[i + 1]))
					{
						decInteger = decInteger + (check[i + 1] - check[i]);
						i++;
					}
					else
						decInteger += check[i];
						i++;
				}
			}
		return 0;
		}


	void setRoman(char romLet)
	{
		romLet = romNum;
	}

//function to return decInteger
	int getRoman()
	{

		return decInteger;
	}


private:
	char romNum;
	
};

int main()
{
	//index to hold the user entered values
	char romanNum[index];

	//explains the program to user
		cout << "This program will take a Roman numeral then, " << endl
			 << "return it's value as a decimal integer." << endl << endl;
		cout << "The value of Roman numerals:" << endl;
		cout << "M = 1000" << endl
			 << "D =  500" << endl
			 << "C =  100" << endl
			 << "L =  50" << endl
			 << "X =  10" << endl
			 << "V =   5" << endl
			 << "I =   1" << endl << endl;

		//user input prompt
		cout << "To exit, type E as your first entry." << endl;

		cin >> romanNum;

			//loops to read each character input individually
			for (int i = 0; i < index; i++)
			{
				//ensures all input is upper case when read
				romanNum[i] = toupper(romanNum[i]);

				//gives the user the option to exit early
				if (romanNum[0] == 'E')
				{
					cout << "Exiting program." << endl;

					//don't forget to delete this
					cin.ignore(2);
					return '\0';
				}

			}

		//setting constructer as the user input values
	 romanConvert converter(romanNum);

		//prints the value of decInteger
	  cout << converter.getRoman();

		


	//don't forget to delete this
	cin.ignore(2);

	return 0;
}

//constructor definition
romanConvert::romanConvert(char romNum[index])
{
	
}
Why not? How far through the function does it get? Does it get to the point of changing the value? If you don't know, find out. Add logging.

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
char romanArray(char romNum[index])
	{
cout << "Entering function romanArray" << '\n';
		//used to populate romNum
		for (int i = 0; i < index; i++)
		{
			if (romNum[i] != '\0')
				romLen++;
			else
				i = index;
		}

cout << "romLen= " << romLen << '\n';
		//finds the number of characters input by the user
		for (int i = 0; i < romLen; i++)
		{
cout << "Loop A, " << i<< '\n';
			good = false;

cout << "romInd= " <<  romInd << '\n';
			//converts the roman number to an integer
			for (int x = 0; x < romInd; x++)
			{

cout << "Loop B, " <<  x<< '\n';
				if (romNum[index] == romLet[i])
				{
					good = true;
				}
			}
cout << "romLen check 2, = " <<  romLen << '\n';
				for (int i = 0; i < romLen; i++)
				{
cout << "Loop C, " <<  i<< '\n';
cout << "romInd = " <<  romInd<< '\n';
					for (int x = 0; x < romInd; x++)
					{
cout << "Loop D, x= " <<  x<< '\n';
						if (romNum[i] == romLet[x])
						{
							check[i] = romVal[x];
						}
					}
				}

              //alters the actual value of decInteger based on the information from check
				for (int i = 0; i < romLen; i++)
				{
					if ((check[i] < check[i + 1]))
					{
						decInteger = decInteger + (check[i + 1] - check[i]);
						i++;
					}
					else
						decInteger += check[i];
						i++;
				}
			}
		return 0;
		}
I feel pretty dumb, but romanArray wasn't altering decInteger because I forgot to include a call to romanArray in main. Thanks for your help.
Topic archived. No new replies allowed.