Need assistance please!

Hi everyone!

I just started my first year in C++ programming, and I'm having a little bit of an issue with my code.

If you guys would kindly look it over and provide some pointers, that would be really great!

Im supposed to write a program that translates between normal Arabic numbers to Roman numeral. The program has to ask the user which system they would like to translate a year to by selecting "A" for arabic, "R" for Roman and "X" to exit the program.

when selecting Roman, they would be able to choose between 1000-3000.

When translating to Arabic, the user can choose between I, V, X, L, C, D, M ( 1, 5, 10, 50, 100, 500, 1000)

The program has to loop to let the user perform multiple calculations.

Thanks in advance!

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
  #include <iostream>
using namespace std;

int main() {
	int k, h, t, o, x;	
	cin >> x;

	if (x > 3000) {
		return 0;
	}

	t = x / 1000;
	if (t >= 1) {
		while (t > 0) {
			cout << "M";
			t--;
		}
	}
	else
		return 0;

	x = x % 1000;
	h = x / 100;
	if (h == 9)
		cout << "CM";
	else if (h == 4)
	cout << "CD";
	else if (h >= 5) {
		cout << "D";
		h = h - 5;
		while (h <= 3 && h > 0) {
			cout << "C";
			h--;
		}
	}
	else if (h > 0) {
		while (h <= 3 && h > 0) {
			cout << "C";
			h--;
		}
	}
	x = x % 100;
	t = x / 10;
	if (t == 9)
		cout << "XC";
	else if (t == 4)
		cout << "CL";
	else if (t >= 5) {
		cout << "L";
		t = t - 5;
		while (t <= 3 && t > 0) {
			cout << "X";
			t--;
		}
	}
	else if (t > 0) {
		while (t <= 3 && t > 0) {
			cout << "X";
			t--;
		}
	}
	x = x % 10;
	o = x / 1;
	if (o == 9)
		cout << "IX";
	else if (o == 4)
		cout << "IV";
	else if (o >= 5) {
		cout << "V";
		o = o - 5;
		while (o <= 3 && o > 0) {
			cout << "I";
			o--;
		}
	}
	else if (o > 0) {
		while (o <= 3 && o > 0) {
			cout << "I";
			o--;
		}
	}
	

	
	cout << endl;
	return 0;
}
closed account (48T7M4Gy)
What you need to do is:
1. add some comments to show what the program is doing
2. use better variable names - single letters are not good. If you use meaningful names the code is largely self-documenting
3. There are no user prompts to say what a user must do. I typed in 3 and nothing happened, then 3000, then 5000 and the program terminated. All I got was a blinking cursor until I pressed return and then termination, it did nothing as far as I could see.
4. gives us a sample of what it expects as input and what you get as output

Cheers
:)
Thanks kemort. I re-worked it all from scratch and had it running arabic to numerals but cant figure out roman to arabic???

Anyways, my friend messed around with the code and now I cant get rid of 2 little errors :(

one is 'while' for int arabicToRoman and the other is expected a '('....

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

using namespace std;

int arabicToRoman(int input);

int main() {

	char inputChar;
	int arabicIn;
	int romanIn;
	int beforeChar;
	int afterChar;
	int romanToArabic;


	do {
		cout << " Press A for Arabic to Roman Numerals" << endl;
		cin >> inputChar;

		switch (inputChar) {
		case 'a':
		case 'A':
			cout << "Select your Arabic Numeral (1000-3000):" << endl;
			cin >> arabicIn;
			arabicToRoman(arabicIn);
			break;

		case 'r':
		case 'R':
			

			cout << "X";
			break;
		default:
			cout << inputChar;
		}
	}

	/** where error is**/
int arabicToRoman(int input) 
		{

			int roman1 = 0;
			int roman2 = 0;
			int roman3 = 0;
			int roman4 = 0;

			if (input > 3000)
			{
				cout << "The number you chose was too high :(";
				return 0;
			}
			else if (input < 1000)
			{
				cout << "The number you chose was too low :(";
				return 0;
			}

			roman1 = input % 10;
			input = input / 10;
			roman2 = input % 10;
			input = input / 10;
			roman3 = input % 10;
			input = input / 10;
			roman4 = input % 10;
			input = input / 10;

			/**------------------------M---------------------------**/
			for (int a = 0; a < roman4; a++)
				cout << "M";

			/**---------------------C to CM--------------------------**/
			if (roman3 >= 1 && roman3 <= 3)
				for (int a = 0; a < roman3; a++)
					cout << "C";

			else if (roman3 == 4)
				cout << "CD";

			else if ((roman3 >= 5) && (roman3 <= 8))
			{
				cout << "D";
				if (roman3 == 5)

					for (int a = 5; a < roman3; a++)
						cout << "C";
			}
			else if (roman3 == 9)
				cout << "CM";
			/**----------------------------X to XC--------------------------------**/

			if (roman2 >= 1 && roman2 <= 3)
				for (int a = 0; a < roman2; a++)
					cout << "X";

			else if (roman2 == 4)
				cout << "XL";

			else if ((roman2 >= 5) && (roman2 <= 8))
			{
				cout << "L";
				if (roman2 == 5)

					for (int a = 5; a < roman2; a++)
						cout << "X";
			}
			else if (roman2 == 9)
				cout << "XC";

			/**----------------------------I to X-------------------------------**/

			if (roman1 >= 1 && roman1 <= 3)
				for (int a = 0; a < roman1; a++)
					cout << "I";

			else if (roman1 == 4)
				cout << "IV";

			else if ((roman1 >= 5) && (roman1 <= 8))
			{
				cout << "V";
				if (roman1 == 5)

					for (int a = 5; a < roman1; a++)
						cout << "I";
			}
			else if (roman1 == 9)
				cout << "IX";
			/**-----------------------------------------------------------**/

			cout << endl;
			return 0;

		}and //**error #2**// 
Last edited on
closed account (48T7M4Gy)
main has to end with

return 0;
}

then you go on with the function descritions. What you appear to have is a } in the wrong place (eg at line 135 - wrong!!) see if that fixes it.
sorry where should i put:

return 0;
}

return 0; should be place before the enclosing bracket of int main.

Example:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main ()
{
     cout << "Hello" << endl;

      return 0; // It is placed before the enclosing bracket of main.
} // Enclosing bracket of main. 
It is before the enclosing bracket of main?

return 0;
}

It keeps showing line 41 as an error still along with line 135?
Line 38: You need to end the do-loop started at line 17. You will also need to end the function, prbably
1
2
  } while (inputChar != 'X');
}


I think you can do Roman numbers to Arabic like this:
1
2
3
4
5
6
7
8
9
10
int total = 0;
for each letter {
    int val = Get the letter's value
    if (there is a letter after this one and
        the next letter's value is more than the current letter's value) {
        val = -val;
    }
    total += val;
}
return total; 

Okay... fixed it... Runs great....

except when Im running arabic to roman numerals and enter any letter, it breaks :(

any ideas?

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
208
209
210
211
212
213
214
215
#include <iostream>

using namespace std;

int arabicToRoman(int input);

int main()
{
	char inputChar;
	int arabicIn = 0;
	int resultArabic = 0;
	char beforeChar = '0'; /*the digit before V, X, etc... The 'I' in IV.*/
	char iRoman = '0'; /*Roman Numeral Input*/

	while (true)
	{
		cout << " Press A for Arabic, R for Roman or X to Exit :";
		cin >> inputChar;

		switch (inputChar) {
		case 'a':
		case 'A':
			cout << "Select an Arabic Number from 1000-3000 :";
			cin >> arabicIn;
			arabicToRoman(arabicIn);
			break;

		case 'r':
		case 'R':
			cout << "Select a Roman Numeral from I-MMM :";
			resultArabic = 0;

			cin.get(iRoman); 
			while (cin.get(iRoman))
			{
				switch (iRoman)
				{
				case 'm':
				case 'M':
					if ((beforeChar == 'C') || (beforeChar == 'c'))
						resultArabic += 800;
					else
						resultArabic += 1000;
					break;

				case 'c':
				case 'C':
					if ((beforeChar == 'x') || (beforeChar == 'X'))
						resultArabic += 80;
					else
						resultArabic += 100;
					break;

				case 'd':
				case 'D':
					if ((beforeChar == 'C') || (beforeChar == 'c'))
						resultArabic += 300;
					else
						resultArabic += 500;
					break;

				case 'x':
				case 'X':
					if ((beforeChar == 'i') || (beforeChar == 'I'))
						resultArabic += 8;
					else
						resultArabic += 10;
					break;

				case 'l':
				case 'L':
					if ((beforeChar == 'X') || (beforeChar == 'x'))
						resultArabic += 30;
					else
						resultArabic += 50;
					break;

				case 'i':
				case 'I':
					resultArabic += 1;
					break;

				case 'v':
				case 'V':
					if ((beforeChar == 'i') || (beforeChar == 'I'))
						resultArabic += 3;
					else
						resultArabic += 5;
					break;
				}
				beforeChar = iRoman;

				if (iRoman == '\n')
					break;
			}
			if (resultArabic >= 0 && resultArabic <= 3000)
				cout << resultArabic << endl;
			else
				cout << "Out" << endl;
			break;


		case 'x':
		case 'X':
			cout << "End of Line." << endl;
			return 0;
		default:
			cout << inputChar;
			break;
		}
	}


	return 0;
}


int arabicToRoman(int input)
{
	int romNum1 = 0;
	int romNum2 = 0;
	int romNum3 = 0;
	int romNum4 = 0;

	if (input > 3000)
	{
		cout << " That's too high of a number." << endl;
		return 0;		
	}
	if (input < 1000)
	{
		cout << " That's too low of a number." << endl;
		return 0;
	}
	romNum1 = input % 10;
	input = input / 10;
	romNum2 = input % 10;
	input = input / 10;
	romNum3 = input % 10;
	input = input / 10;
	romNum4 = input % 10;



/*------------------------------[M (1000)]-----------------------------------------------------------------------------*/

	for (int a = 0; a < romNum4; a++)
		cout << "M";


	/*----------------------------------------[C - CMXCIX (100 - 999)}-------------------------------------------------------------------*/
	if ((romNum3 >= 1) && (romNum3 <= 3))
		for (int a = 0; a < romNum3; a++)
			cout << "C";
	else if (romNum3 == 4)
		cout << "CD";
	else if ((romNum3 >= 5) && (romNum3 <= 8))
	{
		cout << "D";

		if (romNum3 > 5)
			for (int a = 5; a < romNum3; a++)
				cout << "C";
	}
	else if (romNum3 == 9)
		cout << "CM";

	/*----------------------------------------[X - L - XC (10 - 50 - 99)}-------------------------------------------------------------------*/





	if ((romNum2 >= 1) && (romNum2 <= 3))
		for (int a = 0; a < romNum2; a++)
			cout << "X";
	else if (romNum2 == 4)
		cout << "XL";
	else if ((romNum2 >= 5) && (romNum2 <= 8))
	{
		cout << "L";

		if (romNum2 > 5)
			for (int a = 5; a < romNum2; a++)
				cout << "X";
	}
	else if (romNum2 == 9)
		cout << "XC";


	/*----------------------------------------[I - IX (1 - 9)}-------------------------------------------------------------------*/



	if ((romNum1 >= 1) && (romNum1 <= 3))
		for (int a = 0; a < romNum1; a++)
			cout << "I";
	else if (romNum1 == 4)
		cout << "IV";
	else if ((romNum1 >= 5) && (romNum1 <= 8))
	{
		cout << "V";

		if (romNum1 > 5)
			for (int a = 5; a < romNum1; a++)
				cout << "I";
	}
	else if (romNum1 == 9)
		cout << "IX";
	/*-----------------------------------------------------------------------------------------------------------*/

	cout << endl;
	return 0;
}
closed account (48T7M4Gy)
These lines are causing trouble. Comment them out and I get the following output which removes the offending prompts with your version.

1
2
3
4
5
6
			
		//	if (resultArabic >= 0 && resultArabic <= 3000)
		//		cout << resultArabic << endl;
		//	else
		//		cout << "Out" << endl;
		//	break; 


 Press A for Arabic, R for Roman or X to Exit :a
Select an Arabic Number from 1000-3000 :2100
MMC
 Press A for Arabic, R for Roman or X to Exit :a
Select an Arabic Number from 1000-3000 :2100
MMC
 Press A for Arabic, R for Roman or X to Exit :a
Select an Arabic Number from 1000-3000 :2978
MMCMLXXVIII
 Press A for Arabic, R for Roman or X to Exit :x
End of Line.
 
Exit code: 0 (normal program termination)
thanks.

For my assignment I also have to split up the code so one does arabic to roman and another roman to arabic.

Cant seem to get roman to arabic running properly. It won't print the result??

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

#include <iostream>
using namespace std;


int main() {


	
	char inputChar = 0;
	int resultArabic = 0;
	char beforeChar = '0';
	char romIn = '0';

	
	cout << " Press A for Arabic, R for Roman or X to Exit :";
	cin >> inputChar;

	switch (inputChar) {

	case 'r':
	case 'R':
		cout << "Select a Roman Numeral from I-MMM :";
		resultArabic = 0;
		
		cin.get(romIn);
		while (cin.get(romIn) && romIn != '\n') {
			switch (romIn) {
			case 'm':
			case 'M':
				if ((beforeChar == 'C') || (beforeChar == 'c'))
					resultArabic += 800;
				else
					resultArabic += 1000;
				break;
			
			case 'c':
			case 'C':
				if ((beforeChar == 'x') || (beforeChar == 'X'))
					resultArabic += 80;
				else
					resultArabic += 100;
				break;

			case 'd':
			case 'D':
				if ((beforeChar == 'C') || (beforeChar == 'c'))
					resultArabic += 300;
				else
					resultArabic += 500;
				break;

			case 'x':
			case 'X':
				if ((beforeChar == 'i') || (beforeChar == 'I'))
					resultArabic += 8;
				else
					resultArabic += 10;
				break;

			case 'l':
			case 'L':
				if ((beforeChar == 'X') || (beforeChar == 'x'))
					resultArabic += 30;
				else
					resultArabic += 50;
				break;

			case 'i':
			case 'I':
				resultArabic += 1;
				break;

			case 'v':
			case 'V':
				if ((beforeChar == 'i') || (beforeChar == 'I'))
					resultArabic += 3;
				else
					resultArabic += 5;

				break;

				beforeChar = romIn;

				if (romIn == '\n')
					break;
							
				if (resultArabic >= 0 && resultArabic <= 3000)
					cout << resultArabic << endl;
				else
					cout << "I'm sorry Dave, I can't do that." << endl;
				break;

			default:
				cout << resultArabic;
				break;
			}
		}

	
	}
	return 0;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
						cout << "I'm sorry Dave, I can't do that." << endl;
				break;

			default:
				cout << resultArabic;
				break;
			}
			
			cout << resultArabic; //<----------------------------
		}

	
	}
	return 0;
}


I'm not 100% sure but this is a start.

 
Press A for Arabic, R for Roman or X to Exit :R
Select a Roman Numeral from I-MMM :M
1000 
Last edited on
Topic archived. No new replies allowed.