Plethora of Issues

Hello folks. I have been running into many issues with this code. I know that it is wrong but I do not know why. Any advice is appreciated.

-=+=-
You are to design and implement a Roman numeral calculator. The subtractive Roman numeral notation commonly in use today was used only rarely during the time of the Roman Republic and Empire. For ease of calculation, the Romans most frequently used a purely additive notation in which a number was simply the sum of its digits (4 equals IIII in this notation, not IV). Each number starts with the digit of highest value and ends with the digit of smallest value. This is the notation you will use in this program.

Your program inputs two Roman numbers and an arithmetic operator and prints out the result of the operation, also as a Roman number. The values of the Roman digits are as follows:

I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Thus, the number MDCCCCLXXXXVI represents 1996, because 1996 really consists of:

1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1.
M D C C C C L X X X X V I

The arithmetic operators that your program should recognize in the input are +, -, *, and /. These should perform the C++ operations of integer addition, subtraction, multiplication, and division.

One way of approaching this problem is to convert the Roman numbers into integers, then perform the required operation, and then convert the result back into a Roman number for printing.

Assume that the input numbers are in purely additive form - that is, digits are followed only by digits of the same or lower value. Also assume that the letters are all legal, no need to check for errors in the input file. Also, assume that the answer to each calculation will be a positive number.

REQUIREMENTS: This program is to be done using functions. These functions must NOT reference global variables directly (use parameter lists). The prototypes and descriptions below are a suggested method of breaking up the problem into functions – if you prefer to design your own functions, feel free to do so, but make sure you have both value-returning and void functions in your solution.

FUNCTION get_Data
This function receives the input file, reads one series of chars representing a Roman numeral, and sends back the value of the numeral read in. This function can call the function convert_from_Roman_to_Decimal to do the conversion while it is reading each letter.

FUNCTION convert_from_Roman_to_Decimal
int convert_from_Roman_to_Decimal(char);
This function is to receive a char (e.g. an 'M' or a 'C' etc.) and return its corresponding integer value as an integer. Use a value-returning function. It can be called from the get_Data function.

FUNCTION convert_from_ Decimal_ to _ Roman
string convert_from_Decimal_to_Roman(int);
This function is to receive a integer (e.g. 13) and return its corresponding roman value as a string (e.g. XIII). Use a value-returning function.

FUNCTION get_Oper
char get_Oper(ifstream&);
This function receives the input file, reads the operator, and sends back the character read in.
FUNCTION calc_Romans
void calc_Romans(int, int, char, int&);
This function is given the two integers and a char (the operator) and returns the result of doing the required operation on the two integers, (using the reference 4th parameter.)

FUNCTION print_Result
void print_Result(int);
This void function receives the integer result of the calculation, and prints it out. It does not have to return anything to the calling program.

INPUT/OUTPUT: The input file will have number of lines. Each line will have two Roman numbers followed by an operator, separated by blanks. Include a copy of the file with your program and output. The style of the data file looks as follows:

MCCXXVI CV +
MCCXXVI MCCXXVI /
...
etc.

Output Example:
The two lines above would produce the output:

The first number is MCCXXVI ( 1226 ).
The second number is CV ( 105 ).
The operator is +
The result is 1331 ( MCCCXXXI ).
************************************************

The input file (mp4romanletrdata.txt) has the following data:

MCCXXVI CV +
MCCXXVI MCCXXVI /
V I -
MDCLXVI III *
DL DXXXXVIII -
D L /
MDI CXI +
XXV IIII /
XI CII *

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//Goal of the program is to take 2 roman numeral values from a
//text file and compute them in decimal form using the operator
//that is also given in the text file

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Declare the variables
//Or recieve an error message

int convert_from_roman_to_decimal(char);
int convert_from_decimal_to_roman(int);
void get_data(ifstream&, int&, int&, char&);
void print_result(int);

int main() {

//This section initializes the variables and
//opens the file that contains the roman numerals

	int result;
	int number1 = 0;
	int number2 = 0;
	string roman = "";
	char oper;

	ifstream stream;
	stream.open("RomanNumerals.txt");
}

int convert_from_roman_to_decimal(char roman)
{

//This section takes each possible value of the string and computes it into number form,
//than moves to the next letter until there are none left, returning 0

	switch (roman) {
	case 'M':
		return 1000;
	case 'D':
		return 500;
	case 'C':
		return 100;
	case 'L':
		return 50;
	case 'X':
		return 10;
	case 'V':
		return 5;
	case 'I':
		return 1;
	}
	return 0;
}

string convert_from_decimal_to_roman(int number)
{

//This section uses division and modulus to calculate the decimal from the roman
//Starting with the 1000s and then working down to 100s then 10s then single digits

string roman = "";
int digit = number / 1000;

if (digit == 3) {
	roman = "MMM";
}
else if (digit == 2) {
	roman = "MM";
}
else if (digit == 1) {
	roman = "M";
}
digit = (number % 1000) / 100;

if (digit == 9) {
	roman += "CM";
}
else if (digit == 4) {
	roman += "CD";
}
else {
	if (digit >= 5) {
		roman += "D";
		digit -= 5;
	}
	if (digit == 3) {
		roman += "CCC";
	}
	else if (digit == 2) {
		roman += "CC";
	}
	else if (digit == 1) {
		roman += "C";
	}
}
digit = (number % 100) / 10;
if (digit == 9) {
	roman += "XC";
}
else if (digit == 4) {
	roman += "XL";
}
else {
	if (digit >= 5) {
		roman += "L";
		digit -= 5;
	}
	if (digit == 3) {
		roman += "XXX";
	}
	else if (digit == 2) {
		roman += "XX";
	}
	else if (digit == 1) {
		roman += "X";
	}
}
digit = number % 10;

if (digit == 9) {
	roman += "IX";
}
else if (digit == 4) {
	roman += "IIII";
}
else {
	if (digit >= 5) {
		roman += "V";
		digit -= 5;
	}
	if (digit == 3) {
		roman += "III";
	}
	else if (digit == 2) {
		roman += "II";
	}
	else if (digit == 1) {
		roman += "I";
		}
	}
return roman;
}

void calc_romans(int number1, int number2, char oper, int&)
{

//Take the oper variable and determine which one of four posibilities it can be
//Once it figures it out it computes number1 and number2 with the given operation

	int result;

	switch (oper)
	{
	case '+':
		result = number1 + number2;
		break;
	case '-':
		result = number1 - number2;
		break;
	case '/':
		result = number1 / number2;
		break;
	case '*':
		result = number1 * number2;
	default:
		result = 1;
	}

	print_result(result);
	number1 = 0;
	number2 = 0;
}

void get_data(ifstream stream, int number1, int number2, char oper)
{

//Section supposed is supposed to get data from the file
//with string a & b in place of number1 & number2

	string a;
	string b;

	stream >> a;
	stream >> b;
	stream >> oper;

	for (char roman : a) {
		number1 += convert_from_roman_to_decimal(roman);
	}
	for (char roman : b) {
		number2 += convert_from_roman_to_decimal(roman);
	}

}

void print_result(int number1, int number2, char oper, string roman, int number)
{
	cout << "The first number is " << convert_from_decimal_to_roman(number1) << " ( " << number1 << " )." << endl;
	cout << "The second number is " << convert_from_decimal_to_roman(number2) << " ( " << number2 << " )." << endl;
	cout << "The operator is " << oper << endl;
	cout << "The result is " << number << "( " << convert_from_decimal_to_roman(number) << " )" << endl;
	cout << "*******************************" << endl;
}
Last edited on
Look I know y'all cannot do homework and such but I have supplied the code and just need helpful revisions at this point.

One error I am having is that I cannot return the roman variable in the convert_from_decimal_to_roman() function (hence why decimal is currently in its place).

But there is code online that is complete but I do not want to copy that code and would rather just figure out why my infile code is wrong as well as my output.
I cannot return the roman variable in the convert_from_decimal_to_roman() function (hence why decimal is currently in its place).


You're trying to return a string, yes? So spot the problem here:
int convert_from_decimal_to_roman(int number)
-Code Updated

Ah yes that was a dumb problem. But how can I make it so that I can actually get an output? I know that I do not have the proper code for that but do not know how to properly call the functions based off of the infile.
Last edited on
Your code still doesn't compile:

In function 'int main()': 24:6: warning: unused variable 'result' [-Wunused-variable] 
25:6: warning: unused variable 'number1' [-Wunused-variable] 
26:6: warning: unused variable 'number2' [-Wunused-variable] 
28:7: warning: unused variable 'oper' [-Wunused-variable] 
In function 'std::string convert_from_decimal_to_roman(int)': 
59:48: error: ambiguating new declaration of 'std::string convert_from_decimal_to_roman(int)' 
15:5: note: old declaration 'int convert_from_decimal_to_roman(int)' At global scope: 
200:63: warning: unused parameter 'roman' [-Wunused-parameter]
Topic archived. No new replies allowed.