Why wont my program work??

I am working on a project which consists of reading input from a file called input.txt which has an infinite number of lines. Each line has a roman numeral, a space, a plus or minus, another space, and then another roman numeral. I am supposed to add the two numbers together and write the answer to another file called output.txt. However, the program is not working and I dont understand why. When I debug the program in visual studio, I just get a blank console and it does not say "press any key to continue". When I check my output file, it too is blank. Here is what I have...

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

void numeralstonumber(int&, int&, char&);
int calculateanswer(int, int, char);
void answertonumerals(int);

int main()
{
ifstream inputFile;
inputFile.open("input.txt");
while (!inputFile.eof())
{
    int number1 = 0;
    int number2 = 0;
    int answer = 0;
    char operator1;
    numeralstonumber(number1, number2, operator1);
    answer = calculateanswer(number1, number2, operator1);
    answertonumerals(answer);
}
inputFile.close();
}

void numeralstonumber(int &number1, int &number2, char &operator1)
{
ifstream inputFile1;
inputFile1.open("input.txt");
char numeral = ' ';
char nextnumeral;
while (numeral != '+' && numeral != '-')
{
    inputFile1 >> numeral;
    nextnumeral = cin.peek();
    if (nextnumeral == '-' || nextnumeral == '+')
    {
        nextnumeral = operator1;
    }
    else if (numeral == 'I' && (nextnumeral == 'X' || nextnumeral == 'V'))
    {
        number1 = number1 - 1;
    }
    else if (numeral = 'X' && (nextnumeral == 'L' || nextnumeral == 'C'))
    {
        number1 = number1 - 10;
    }
    else if (numeral == 'I')
    {
        number1 = number1 + 1;
    }
    else if (numeral == 'V')
    { 
        number1 = number1 + 5;
    }
    else if (numeral == 'X')
    {
        number1 = number1 + 10;
    }
    else if (numeral == 'L')
    {
        number1 = number1 + 50;
    }
    else if (numeral == 'C')
    {
        number1 = number1 + 100;
    }
}
while (numeral != '\n')
{
    inputFile1 >> numeral;
    nextnumeral = cin.peek();
    if (numeral == 'I' && (nextnumeral == 'X' || nextnumeral == 'V'))
    {
        number2 = number2 - 1;
    }
    else if (numeral = 'X' && (nextnumeral == 'L' || nextnumeral == 'C'))
    {
        number2 = number2 - 10;
    }
    else if (numeral == 'I')
    {
        number2 = number2 + 1;
    }
    else if (numeral == 'V')
    {
        number2 = number2 + 5;
    }
    else if (numeral == 'X')
    {
        number2 = number2 + 10;
    }
    else if (numeral == 'L')
    {
        number2 = number2 + 50;
    }
    else if (numeral == 'C')
    {
        number2 = number2 + 100;
    }
}
inputFile1.close();
}

int calculateanswer(int number1, int number2, char operator1)
{
int answer;
if (operator1 == '+')
{
    answer = number1 + number2;
}
else if (operator1 == '-')
{
    answer = number1 - number2; 
}
return answer; 
}

void answertonumerals(int answer)
{
string numeralanswer;
if (answer / 100 == 2)
{
    numeralanswer = "CC";
}
else if (answer / 100 == 1)
{
    numeralanswer = "C";
}
if ((answer % 100) / 10 == 9)
{
    numeralanswer = numeralanswer + "XC";
}
else if ((answer % 100) / 10 == 8)
{
    numeralanswer = numeralanswer + "LXXX";
}
else if ((answer % 100) / 10 == 7)
{
    numeralanswer = numeralanswer + "LXX";
}
else if ((answer % 100) / 10 == 6)
{
    numeralanswer = numeralanswer + "LX";
}
else if ((answer % 100) / 10 == 5)
{
    numeralanswer = numeralanswer + "L";
}
else if ((answer % 100) / 10 == 4)
{
    numeralanswer = numeralanswer + "XL";
}
else if ((answer % 100) / 10 == 3)
{
    numeralanswer = numeralanswer + "XXX";
}
else if ((answer % 100) / 10 == 2)
{
    numeralanswer = numeralanswer + "XX";
}
else if ((answer % 100) / 10 == 1)
{
    numeralanswer = numeralanswer + "X";
}
if ((answer % 10) == 9)
{
    numeralanswer = numeralanswer + "IX";
}
else if ((answer % 10) == 8)
{
    numeralanswer = numeralanswer + "VIII";
}
else if ((answer % 10) == 7)
{
    numeralanswer = numeralanswer + "VII";
}
else if ((answer % 10) == 6)
{
    numeralanswer = numeralanswer + "VI";
}
else if ((answer % 10) == 5)
{
    numeralanswer = numeralanswer + "V";
}
else if ((answer % 10) == 4)
{
    numeralanswer = numeralanswer + "IV";
}
else if ((answer % 10) == 3)
{
    numeralanswer = numeralanswer + "III";
}
else if ((answer % 10) == 2)
{
    numeralanswer = numeralanswer + "II";
}
else if ((answer % 10) == 1)
{
    numeralanswer = numeralanswer + "I";
}

ofstream outputFile;
outputFile.open("output.txt");
outputFile << numeralanswer << endl;
outputFile.close();
}
morning,
which has an infinite number of lines

That would take a very long time to process :)

When you debug it, have you stepped trough the program line by line? It shouldn't take too long.
I just get a blank console and it does not say "press any key to continue"


This implies that none of your if conditions are evaluating to true, and thefore it's stepping over everything. So yea, wack a breakpoint on line 34, then step line by line (F10 in VS).

Edit: You might be better reading each whole line into a std::string and parsing it like that. I'm not sure calling peek() is a good method to extract characters, but I might be wrong:

http://stackoverflow.com/questions/13501862/how-to-properly-use-cin-peek
Last edited on
Topic archived. No new replies allowed.