Outputting to a .txt file

I have been working on a simple program that will perform encryption/decryption based on a simple 1:1 pattern. You enter the program, choose the encryption pattern you prefer (which currently only supports one option) and then choose to encrypt or decrypt your text. When you choose to encrypt, you are prompted to enter text.
The program then reads the character inputs that you enter, and uses a repeating switch loop to translate each letter into its encrypted letter (a becomes z, b becomes y, etc) and then prints the encrypted text to the screen. at the same time, it writes the encrypted text to an external .txt file. this part of the program works fine. However, when I go to decrypt my text, i have a problem. The decryptor reads the text from the external file into an array, and then translates it one character at a time, in the same fashion as the encryptor. however, it will not write back to the txt file. i have also tried to write the decrypted text to a new txt file, and the program crashes at decryption time.
when i am attempting to write the decrypted text back to the original txt file, the decrypted text does print to the screen, but does not write to the txt file. no error messages or crashes are presented/occur in this case.

i know that my coding style is imperfect, and that it doesn't follow all accepted formatting protocol. I am only looking for an idea as to why the second write function does not carry out. Any suggestions would be greatly appreciated. Thanks in advance.

Main menu .cpp file: (only needed if you intend to run the file yourself)

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
#include<iostream>
#include <fstream>
#include <string>
#include"HalfFold.h"
using namespace std;


int main( void )
{																//open main
	

	char pattern='a';									//encryption pattern selection
	char encDec='a';									//encryption/decryption selector
	int incOrDec=0;
    
	Code myCode;
	
	cout<< "Hello, welcome to the encryptor!" << endl;
	cout<< "When typing a sentence for encryption, remember to use \n a comma (,) instead of a space between words."<<endl;
	cout<< "When you have finished using an encryption method, enter 1"<<endl<<endl;

	
	for (int codeSelect=1; codeSelect>0; codeSelect++){		//open code select For

	cout<< "Please select cryptographic pattern:" << endl << "a= geocache style half-fold" << endl;
	cout<< "b= random pattern" <<endl;

		cin>>pattern;

		cout<<endl;

		switch (pattern){									//open Switch

			case'a':
			cout<< "Select operation: \n e=encrypt   d=decrypt"<< endl;
			while (incOrDec==0)
			incOrDec++;
			cin>> encDec;
			if(!encDec== 'e' || !encDec== 'd')
			{
				cout<<"Invalid Entry. Re-enter selection"<<endl;
				incOrDec=0;
			}
			
			if (encDec=='e')
			{
				myCode.foldEnc();
			}

			if (encDec=='d')
			{
				myCode.foldDec();
			}
			break;

			default:
				cout<< "Please review the list of options and make a new selection."<<endl;
				break;
	
		}												//close pattern selection Switch
	}													//close encryption pattern For loop


	return 0;

}														//close main


Header file containing the encryption/decryption program (THIS IS WHERE THE ERROR APPARENTLY LIES):

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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class Code
{														//open class Code

public: 

void foldEnc( void ){										//open fold Encrypt method
	fstream myfile;
	myfile.open("crypto.txt");

	cout<< endl<<  "Enter your text for encryption:"<< endl;

	char in='a';										//letter to swap

	for(int x=1; x>0 ;x++){								//open infinite For loop

		cin >> in;

		switch (in){									//open Switch loop

		case'_':
			cout<< " ";
			myfile<< " " ;
			break;

		case',':
			cout<<",";
			myfile<< "," ;
			break;

		case'a':
			cout<< "z";
			myfile<< "z" ;
			break;

		case'b':
			cout<< "y";
			myfile<< "y" ;
			break;

		case'c':
			cout<< "x";
			myfile<< "x" ;
			break;

		case'd':
			cout<< "w";
			myfile<< "w" ;
			break;

		case'e':
			cout<< "v";
			myfile<< "v" ;
			break;

		case'f':
			cout<< "u";
			myfile<< "u" ;
			break;

		case'g':
			cout<< "t";
			myfile<< "t" ;
			break;

		case'h':
			cout<< "s";
			myfile<< "s" ;
			break;

		case'i':
			cout<< "r";
			myfile<< "r" ;
			break;

		case'j':
			cout<< "q";
			myfile<< "q" ;
			break;

		case'k':
			cout<< "p";
			myfile<< "p" ;
			break;

		case'l':
			cout<< "o";
			myfile<< "o" ;
			break;

		case'm':
			cout<< "n";
			myfile<< "n" ;
			break;

		case'n':
			cout<< "m";
			myfile<< "m" ;
			break;

		case'o':
			cout<< "l";
			myfile<< "l" ;
			break;

		case'p':
			cout<< "k";
			myfile<< "k" ;
			break;

		case'q':
			cout<< "j";
			myfile<< "j" ;
			break;

		case'r':
			cout<< "i";
			myfile<< "i" ;
			break;

		case's':
			cout<< "h";
			myfile<< "h" ;
			break;

		case't':
			cout<< "g";
			myfile<< "g" ;
			break;

		case'u':
			cout<< "f";
			myfile<< "f" ;
			break;

		case'v':
			cout<< "e";
			myfile<< "e" ;
			break;

		case'w':
			cout<< "d";
			myfile<< "d" ;
			break;

		case'x':
			cout<< "c";
			myfile<< "c" ;
			break;

		case'y':
			cout<< "b";
			myfile<< "b" ;
			break;

		case'z':
			cout<< "a";
			myfile<< "a" ;
			break;

		case'1':
			cout<< endl <<endl << "Exiting half fold encryptor."<<endl<<endl<<endl;
			x=-1;
			myfile<< "1" ;
			myfile.close();
			break;

		default:
			cout<< "Invalid entry";
			break;
	}												//close Switch
	
}													//close infinite For

}													//close Fold Decrypt method

void foldDec( void )
{													//open method FoldDec
	fstream myfile;
	myfile.open("crypto2.txt");
	
	int input=0;
	int index=0;
	char  inputArray[1000];
	while ( !myfile.eof() )
	{
		inputArray[index]= myfile.get();
		index++;
	}
	
	myfile.close();

	fstream myDecode;
	myDecode.open("cryptoDecoded.txt");

	for (input=0; input>-1; input++)
	{										//open For input loop

	switch (inputArray[input]){			//open Switch loop

		case'_':
			cout<< " ";
			myDecode<< " " ;
			break;

		case',':
			cout<<" ";
			myDecode<< " " ;
			break;

		case'a':
			cout<< "z";
			myDecode<< "z" ;
			break;

		case'b':
			cout<< "y";
			myDecode<< "y" ;
			break;

		case'c':
			cout<< "x";
			myDecode<< "x" ;
			break;

		case'd':
			cout<< "w";
			myDecode<< "w" ;
			break;

		case'e':
			cout<< "v";
			myDecode<< "v" ;
			break;

		case'f':
			cout<< "u";
			myDecode<< "u" ;
			break;

		case'g':
			cout<< "t";
			myDecode<< "t" ;
			break;

		case'h':
			cout<< "s";
			myDecode<< "s" ;
			break;

		case'i':
			cout<< "r";
			myDecode<< "r" ;
			break;

		case'j':
			cout<< "q";
			myDecode<< "q" ;
			break;

		case'k':
			cout<< "p";
			myDecode<< "p" ;
			break;

		case'l':
			cout<< "o";
			myDecode<< "o" ;
			break;

		case'm':
			cout<< "n";
			myDecode<< "n" ;
			break;

		case'n':
			cout<< "m";
			myDecode<< "m" ;
			break;

		case'o':
			cout<< "l";
			myDecode<< "l" ;
			break;

		case'p':
			cout<< "k";
			myDecode<< "k" ;
			break;

		case'q':
			cout<< "j";
			myDecode<< "j" ;
			break;

		case'r':
			cout<< "i";
			myDecode<< "i" ;
			break;

		case's':
			cout<< "h";
			myDecode<< "h" ;
			break;

		case't':
			cout<< "g";
			myDecode<< "g" ;
			break;

		case'u':
			cout<< "f";
			myDecode<< "f" ;
			break;

		case'v':
			cout<< "e";
			myDecode<< "e" ;
			break;

		case'w':
			cout<< "d";
			myDecode<< "d" ;
			break;

		case'x':
			cout<< "c";
			myDecode<< "c" ;
			break;

		case'y':
			cout<< "b";
			myDecode<< "b" ;
			break;

		case'z':
			cout<< "a";
			myDecode<< "a" ;
			break;

		case'1':
			cout<< endl<< endl<< "Exiting half fold decryptor."<<endl<<endl<<endl;
			input=-2;
			myDecode<< "1";
			myDecode.close();
			break;

		default:
			cout<< "Invalid entry";
			break;
	}												//close Switch


}													//close For input loop

}													//close method foldDec

};													//close class Code 



Thanks again,
Lukipaela
Last edited on
Please put your code in code blocks, I'm trying to read this but it's not easy on the eyes.

to do a code block type [code][/code]
Um... there's a much easier way to map a character to its opposite equivalent.
char neo = ('z' - old + 'a'); //For lowercase letters only. For uppercase letters... guess. ;)

-Albatross
Lukipaela wrote:
I have been working on a simple program
Can we see this simple program? The program you posted is needlessly complex/over-complicated and just plain tedious.
clanmjc- Sorry about that, hope this helps.

Albatross- though I appreciate the tip, i do not know that that method will work. This conversion is simple, and I see that it can be done in faster ways, but after I am able to straighten out the write issue, I will be forming more complex conversions involving short term memory of inputs to determine outputs, and I do not think that this method will apply in that context. as I said, I am only seeking to resolve the write issue, not make the code prettier.
But thank you for taking the time to look into the post.

LB- the action performed by the program is very simple. I didn't say that the code is simple. In the future, try posting constructive comments. Your post was completely useless.
My point is that you have a huge code reuse problem, and I think that was LB's point too. :/

Anyways, check line 202 in your header. Is it just me, or does that condition look a little bit... incomplete? I'm not sure if that is the problem, but it seems you're relying on something that is not guaranteed to exist in your file.

Finally, for the purpose of being explicit, have you considering using ifstreams and ofstreams instead of bi-directional fstreams?

-Albatross
Last edited on
LB- as I said to Albatross, this is only a framework. Once I have straightened out the write issue, which is the topic of this post, and a subject you have not commented on, I will be implementing a more complex system of conversion, rather than just this exceedingly simple one. The code may be repetitive and long, but that is not the source of my problem. I appreciate you taking the time to suggest an alternative conversion method, but it is not what I am seeking assistance with.
Last edited on
Lukipaela wrote:
LB- the action performed by the program is very simple. I didn't say that the code is simple. In the future, try posting constructive comments. Your post was completely useless.
"simple program" this is not,
"simple action" this is.

A program should be as simple as its action.

1
2
3
4
5
6
std::string s ("Hello, world.");
for(std::string::size_type i = 0; i < s.length(); ++i)
{
    if(s[i] > 'a' && s[i] < 'z') s[i] = 'z' - s[i] + 'a';
    if(s[i] > 'A' && s[i] < 'Z') s[i] = 'Z' - s[i] + 'A';
}



EDIT: I did not see your second post. See Albatross' post. She has explained my intentions.
Last edited on
Albatross-
line 202 is the declaration of a for loop. The loop is designed to carry on infinitely many times, until a termination character is encountered. (when someone enters the character '1' in the text). I am not sure what you mean by relying on something that is not guaranteed to exist.
As for the ifstreams/ofstreams, this is the first time I have used these functions (though technically I haven't used either of those two, iI am sure you get my point) so I honestly don't know if I should be using them instead of the fstream. I thought that using the fstream was what i would have wanted because for decrypting, i read from the file and then attempt to write back to it.

EDIT: line 202 issue: Perhaps you meant that the termination character is not guaranteed to exist within the file. That is a great point, and I will have to add a line appending it to the end of the array automatically after loading the encrypted message. However, in testing the program, I have always been careful to use this termination character, because it is not possible to exit encryption and enter decryption without using it.
Last edited on
I have tried this using the myfile object for both encrypt and decrypt, as well as what is pictured above (using myfile object for encryption and myDecode for decryption). I am at a loss as to why the write to .txt function works the in the encryption method, but not decryption.
Topic archived. No new replies allowed.