my code isn't formatting in my file correctly

i have a project due in a few hours. the project was to take a file that had roman numerals and numbers in it and write a program that contained functions to convert roman to numbers and numbers to roman numeral and then write it back to the same file with a specific format. so if my file had IV 5 IX in it, when the program writes it back into the file its suppose to look like this
IV 4
V 5
IX 9.
what i decided to do was write it to a new file and once it reach end of file for the old file it will delete the old file and rename the new one the same name as the old file. but for some reason it doesnt format correctly. for example if my old file had IV X 9 4, it will come out like this
IV
IV 4
X
X 10
IX 9
IV 4
my conversion works the way i want it and i know my code could probably be better but at this point i dont have time to rewrite it. please help me figure out why its doing this.

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 // testing.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <stdio.h>

using namespace std;

void decimalToRoman(string romanNumeral);
void romanConverter(string romanNumeral);

int main()
{
	string line;
	
	ifstream testing("numbers.txt");
	if (testing.is_open())
	{
		
	while (!testing.eof())
	{
		while (testing>> line)
		{

			if (line.at(0) != 'M' || line.at(0) != 'D' || line.at(0) != 'C' || line.at(0) != 'L' || line.at(0) != 'X' || line.at(0) != 'V' || line.at(0) != 'I')
			{
				
				decimalToRoman(line);

			}

			
			 if (line.at(0) == 'M' || line.at(0)== 'D' || line.at(0) == 'C' || line.at(0) == 'L' || line.at(0) == 'X' || line.at(0) == 'V' || line.at(0) == 'I')
			{
				
			romanConverter(line);
			}
			 
		}
			
	}
	testing.close();
	//remove("numbers.txt");
	//rename("temporary.txt", "numbers.txt");
}
	
	
	return 0;
}

void romanConverter(string romanNumeral)
{
	int decimalTotal = 0;
	string words = romanNumeral;
	
	for (unsigned int i = 0; i < words.length(); ++i)
	{

		if (words[i] == 'M')
		{
			decimalTotal += 1000;
		}

		if (words[i] == 'D')
		{
			decimalTotal += 500;
		}

		if (words[i] == 'C')
		{
			if (words[i + 1] != 'C' && (i + 1) != words.length())
				{

					decimalTotal -= 100;
				}
			else
				{
					decimalTotal += 100;
				}
			
		}

		if (words[i] == 'L')
		{
			 
			
			
				decimalTotal += 50;
			
		}

		if (words[i] == 'X')
		{
			if (words[i + 1] != 'X' && (i + 1) != words.length())
				{
				
					decimalTotal -=10;
				}
				

			else
				{
					decimalTotal += 10;
				}
			
		}

		if (words[i] == 'V')
		{ 
			
			
				decimalTotal += 5;
			
		}

		if (words[i] == 'I')
		{
			if (words[i+1] != 'I' && (i+1) != words.length())
			{
				decimalTotal -=1;
			}
			else
			{
				decimalTotal += 1;
			}
			

		
		}
		
		
	}


	ofstream tempFile("temporary.txt", ios::in | ios::out | ios::app);

	tempFile<< words << " " << decimalTotal << endl;
			
}


	void decimalToRoman(string romanNumeral)
	{

		int piece = 0;
		string roman;
		string word = romanNumeral;
		int decimalNumber = atoi(word.c_str());


		if (decimalNumber >= 1000)
		{
			piece = (decimalNumber / 1000);

			for (int i = 0; i < piece; i++)
			{
				roman += 'M';
			}
			decimalNumber %= 1000;
		}

		if (decimalNumber >= 100)
		{
			piece = (decimalNumber / 100);

			if (piece == 9)
			{
				roman += "CM";
			}

			else if (piece >= 5)
			{
				roman += 'D';

				for (int i = 0; i < piece - 5; i++)
					roman += 'C';
			}
			else if (piece == 4)
			{
				roman += "CD";
			}
			else if (piece >= 1)
			{
				for (int i = 0; i < piece; i++)
				{
					roman += 'C';
				}
			}

			decimalNumber %= 100;
		}
		if (decimalNumber >= 10)
		{
			piece = decimalNumber / 10;

			if (piece == 9)
			{
				roman += "XC";

			}
			else if (piece >= 5)
			{
				roman += 'L';
				for (int i = 0; i < piece - 5; i++)
				{
					roman += 'X';
				}
			}
			else if (piece == 4)
			{
				roman += "XL";
			}
			else if (piece >= 1)
			{
				for (int i = 0; i < piece; i++)
				{
					roman += 'X';
				}

			}
			decimalNumber %= 10;
		}
		if (decimalNumber >= 1)
		{
			piece = decimalNumber;
			if (piece == 9)
			{
				roman += "IX";
			}
			else if (piece >= 5)
			{

				roman += "V";
				for (int i = 0; i < piece - 5; i++)
				{
					roman += 'I';
				}
			}
			else if (piece == 4)
			{
				roman += "IV";
			}
			else if (piece >= 1)
			{
				for (int i = 0; i < piece; i++)
				{
					roman += 'I';

				}

			}
		}
		ofstream tempFile("temporary.txt", ios::in |ios::out | ios::app);

		tempFile << roman << " "<<romanNumeral<< endl;
		
	}
Use setw() it might help
@LendraDwi how would that help? could you elaborate a bit please. the final file is formatting correctly its just that if the old file has IV 4 I X
it formats like this:
IV
IV 4
IV 4
I
I 1
X
X 10.

the problem is that the first roman numerals that are by themselves aren't suppose to be there. its like they are being copied over from the old file when they arent supposed to be.
Sorry I miisread some part
I think you need to store the inputted value and return value from both of your functiion, and write them under 1 loop in main
is this really the only way? to be honest im not good with functions yet so this took me a really long time to write and with the few hours i have left i dont think i can rewrite the whole code if i need to.
The problem is this line
1
2
3
4
5
6
			if (line.at(0) != 'M' || line.at(0) != 'D' || line.at(0) != 'C' || line.at(0) != 'L' || line.at(0) != 'X' || line.at(0) != 'V' || line.at(0) != 'I')
			{
				
				decimalToRoman(line);

			}


it should be && instead of ||
1
2
3
4
5
6
				if (line.at(0) != 'M' && line.at(0) != 'D' && line.at(0) != 'C' && line.at(0) != 'L' && line.at(0) != 'X' && line.at(0) != 'V' && line.at(0) != 'I')
				{

					decimalToRoman(line);

				}


Last edited on
Topic archived. No new replies allowed.