code doesn't execute correctly

Hi, everyone, I wrote a program for my intro C++ class question, but my code doesn't executing correctly. I hope I could get help.
Here's the question:

Write a C++ program that accepts a year written as a four-digit Arabic (ordinary) numeral and outputs the year written in Roman numerals.

Important:
Roman numerals are V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1,000.

Recall that some numbers are formed by using a kind of subtraction of one Roman “digit”; for example, IV is 4 produced as V minus I, XL is 40, CM is 900, and so on.

A few sample years: MCM is 1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940, MCMLXXXIX is 1989.

(Hints: Use division and mod.)

Assume the year is between 1000 and 3000. Prompt the user again if the user types less than 1000 or bigger than 3000.

Your program should include a loop that lets the user repeat this calculation until the user says she or he is done.

Define the following functions (value returning functions, Cally-by-Reference):

validateUserInput(): validates the user's input (must be between 1000 and 3000, and integers only).

returnRomanThousandsPlace(): returns roman numerals for the thousand place.

returnRomanHundredsPlace(): returns roman numerals for the hundreds place.

returnRomanTensPlace(): returns roman numerals for the tens place.

returnRomanOnesPlace(): returns roman numerals for the ones place.


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

using namespace std;

void validateUserInput(int &input, bool &t);
void returnRomanThousandsPlace(int &thousands, int &input);
void returnRomanHundredsPlace(int &hundreds, int &x, int &input);
void returnRomanTensPlace(int &tens, int &x, int&y);
void returnRomanOnesPlace(int &ones, int &z, int &y);



int main()
{
    int input;
    bool t;
    int thousands;
    int hundreds, x;
    int tens, y;
    int ones, z;
    char wheel;

    while(cin)
    {
        cout << "This program will accept a year written in four digit ordinary numeral and will" <<endl;
        cout << "convent it into roman numerals." << endl;

        cout << endl;
        cout << endl;

        validateUserInput(input, t);

        cout << "Your number in roman numerals is:" << endl;

        returnRomanThousandsPlace(thousands, input);
        returnRomanHundredsPlace(hundreds, x, input);
        returnRomanTensPlace(tens, x, y);
        returnRomanOnesPlace(ones, z, y);

        cout << endl;
        cout << endl;

        cout << "Do you want to repeat the program again (y/n)?" << endl;
        cin >> wheel;

        cout << endl;
        cout << endl;

        if(wheel = 'y')
            continue;
        else if(wheel = 'n')
            break;

    }

    return 0;
}

//a
void validateUserInput(int &input, bool &t)
{
    t = true;

    cout << "Enter a year between 1000 and 3000:" << endl;
    cin >> input;

    cout << endl;
    cout << endl;

    while(t)
    {
        if ((input >= 1000) && (input <= 3000))
            t = false;
        else
        {
            cout << "The year entered is invalid. Enter a valid year between 1000 to 3000:" << endl;
            cin >> input;
        }
        break;
    }

}

//b
void returnRomanThousandsPlace(int &thousands, int &input)
{
    thousands = input / 1000;

    if(thousands == 1000)
          cout<< "M";
    if(thousands == 2000)
          cout<< "MM";
    if(thousands == 3000)
          cout<< "MMM";
}

//c
void returnRomanHundredsPlace(int &hundreds, int &x, int &input)
{
    x = input %1000;
    hundreds = x / 100;

    if (hundreds == 100)
		 cout<< "C";
	else if (hundreds == 200)
		 cout<< "CC";
	else if (hundreds == 300)
		 cout<< "CCC";
	else if (hundreds == 400)
		 cout<< "CD";
	else if (hundreds == 500)
		 cout<< "D";
	else if (hundreds == 600)
		 cout<< "DC";
	else if (hundreds == 700)
		 cout<< "DCC";
	else if (hundreds == 800)
		 cout<< "DCCC";
	else if (hundreds == 900)
		 cout<< "CM";
}

//d
void returnRomanTensPlace(int &tens, int &x, int&y)
{
    y = x % 100;
    tens = y / 10;

    if (tens == 10)
		 cout<< "X";
	else if (tens == 20)
		 cout<< "XX";
	else if (tens == 30)
		 cout<< "XXX";
	else if (tens == 40)
		 cout<< "XL";
	else if (tens == 50)
		 cout<< "L";
	else if (tens == 60)
		 cout<< "LX";
	else if (tens == 70)
		 cout<< "LXX";
	else if (tens == 80)
		 cout<< "LXXX";
	else if (tens == 90)
		 cout<< "XC";
}

//e
void returnRomanOnesPlace(int &ones, int &z, int &y)
{
    z = y % 10;
    ones = z / 1;

    if (ones = 1)
        cout << "I";
    else if (ones = 2)
        cout << "II";
    else if (ones == 3)
        cout<< "III";
	else if (ones == 4)
        cout<< "IV";
	else if (ones == 5)
        cout<< "V";
	else if (ones == 6)
        cout<< "VI";
	else if (ones == 7)
        cout<< "VII";
	else if (ones == 8)
        cout<< "VIII";
	else if (ones == 9)
        cout<< "IX";
        cout<<endl;
}
You are often making the mistake of saying = (assignment) when you mean == (test for equality). A decent compiler set to a high enough warning level would have automatically pointed out the problem.
Last edited on
well, I change line 155 and 157 ,both use ==(test for equality). However, the result while I compile in codeblock is only shows roman numeral ones place, and IDE doesn't show any error. I don't understand.
The way you are doing it is overly-complicated. I would make all of your so-called "return" functions take a single int (not a reference). Just pass the year to each one and extract the correct digit in each function.

Pseudocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
thousands:
  switch (n / 1000) {
  case 1: ...
  case 2: ...
  }

hundreds:
  switch (n % 1000 / 100) {
  case 1: ...
  case 2: ...
  }

tens:
  switch (n % 100 / 10) {
  case 1: ...
  case 2: ...
  }

ones:
  switch (n % 10) {
  case 1: ...
  case 2: ...
  }

Thanks bro, I'll consider that.
keep at it; this is an excellent exercise! Strange that such limits have been placed on the input years -- isn't the usual roman numerals range from 1 to 3999 ?
Topic archived. No new replies allowed.