Help With Simple Program

I need to create a simple program that takes a phrase and scrambles the letters all up and or unscrambles them. I need to use simple functions and have pretty much completed it but I cannot get my functions to actually go forth and complete what they need to do. The code below will help you understand more of what I need it to do.

The program compiles fine but the spaces where the function output should be are blank. What am I doing wrong?

Header File
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string> //for strings
#include <fstream> //for ifstream and ofstream

using std::string; //using standard lib and string

void encrypt( string input ); //helper encrypt function
void decrypt( string input ); //helper decrypt function

void encryptDecrypt( string inputfile, string outputfile ); //principle function declaration


Actual CPP
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
#include "encrypt.h" //header file
#include <iostream>
#include <string> //for strings
#include <fstream> //for file stream, ofstream and ifstream

using std::endl;
using std::string;
using std::ifstream;//input file stream
using std::ofstream;//output file stream
using std::getline; //reading rest of the line

int main()//main test function
{
    encryptDecrypt( "inputED.txt", "outputEncDec.txt");//test input files
    return 0;
}

void encryptDecrypt(string inputfile, string outputfile) //principle function
{
    ifstream in(inputfile);  //input file
    ofstream out(outputfile);//output file

	string command, command2;
	in >> command;getline( in, command2); //simple extraction, then reading rest of line with loop

		while ( !in.fail() ) //input controlled failure loop
	{
        if ( command == "encrypt" )
        { 
		out << "encrypted:"; encrypt( command2 ); out << '\n';
		}
		else if ( command == "decrypt")
		{
		out << "decrypted:"; decrypt( command2 ); out << '\n';
		}
		
		in >> command; getline( in, command2 );
	}
	
    in.close();
    out.close();
}

void encrypt( string input ) //"helper" encrypt function
{
	string results = "";
	for ( int i=0; i<input.length(); i++ )
{
    if ( input[i] == 'a' )
		{
        results = results + "z";
		}
	else if ( input[i] == 'b' )
	{
		results = results + "y";
	}
	else if ( input[i] == 'c' )
	{
		results = results + "x";
	}
	else if ( input[i] == 'd' )
	{
		results = results + "w";
	}
	else if ( input[i] == 'e' )
	{
		results = results + "v";
	}
	else if ( input[i] == 'f' )
	{
		results = results + "u";
	}
	else if ( input[i] == 'g' )
	{
		results = results + "t";
	}
	else if ( input[i] == 'h' )
	{
		results = results + "s";
	}
	else if ( input[i] == 'i' )
	{
		results = results + "r";
	}
	else if ( input[i] == 'j' )
	{
		results = results + "q";
	}
	else if ( input[i] == 'k' )
	{
		results = results + "p";
	}
	else if ( input[i] == 'l' )
	{
		results = results + "o";
	}
	else if ( input[i] == 'm' )
	{
		results = results + "n";
	}
	else if ( input[i] == 'o' )
	{
		results = results + "l";
	}
	else if ( input[i] == 'p' )
	{
		results = results + "k";
	}
	else if ( input[i] == 'q' )
	{
		results = results + "j";
	}
	else if ( input[i] == 'r' )
	{
		results = results + "i";
	}
	else if ( input[i] == 's' )
	{
		results = results + "h";
	}
	else if ( input[i] == 't' )
	{
		results = results + "g";
	}
	else if ( input[i] == 'u' )
	{
		results = results + "f";
	}
	else if ( input[i] == 'v' )
	{
		results = results + "e";
	}
	else if ( input[i] == 'w' )
	{
		results = results + "d";
	}
	else if ( input[i] == 'x' )
	{
		results = results + "c";
	}
	else if ( input[i] == 'y' )
	{
		results = results + "b";
	}
	else if ( input[i] == 'z' )
	{
		results = results + "a";
	}
	}
}

void decrypt( string input ) //"helper" decrypt function
{
	string results = "";
	for ( int i=0; i<input.length(); i++ )
{
    if ( input[i] == 'z' )
		{
        results = results + "a";
		}
	else if ( input[i] == 'y' )
	{
		results = results + "b";
	}
	else if ( input[i] == 'x' )
	{
		results = results + "c";
	}
	else if ( input[i] == 'w' )
	{
		results = results + "d";
	}
	else if ( input[i] == 'v' )
	{
		results = results + "e";
	}
	else if ( input[i] == 'u' )
	{
		results = results + "f";
	}
	else if ( input[i] == 't' )
	{
		results = results + "g";
	}
	else if ( input[i] == 's' )
	{
		results = results + "h";
	}
	else if ( input[i] == 'r' )
	{
		results = results + "i";
	}
	else if ( input[i] == 'q' )
	{
		results = results + "j";
	}
	else if ( input[i] == 'p' )
	{
		results = results + "k";
	}
	else if ( input[i] == 'o' )
	{
		results = results + "l";
	}
	else if ( input[i] == 'n' )
	{
		results = results + "m";
	}
	else if ( input[i] == 'l' )
	{
		results = results + "o";
	}
	else if ( input[i] == 'k' )
	{
		results = results + "p";
	}
	else if ( input[i] == 'j' )
	{
		results = results + "q";
	}
	else if ( input[i] == 'i' )
	{
		results = results + "r";
	}
	else if ( input[i] == 'h' )
	{
		results = results + "s";
	}
	else if ( input[i] == 'g' )
	{
		results = results + "t";
	}
	else if ( input[i] == 'f' )
	{
		results = results + "u";
	}
	else if ( input[i] == 'e' )
	{
		results = results + "v";
	}
	else if ( input[i] == 'd' )
	{
		results = results + "w";
	}
	else if ( input[i] == 'c' )
	{
		results = results + "x";
	}
	else if ( input[i] == 'b' )
	{
		results = results + "y";
	}
	else if ( input[i] == 'a' )
	{
		results = results + "z";
	}
}
}
Last edited on

The first thing I would consider is killing those {if else} statements, and go with something simpler like switch or arrays. There is an old saying... the more complicated the plumbing, the easier it is to stop up the pipes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void decrypt( string input ) //"helper" decrypt function

switch results{
    case  'z' :
        results =+ "a";
        break;
        
    case 'y' :
        results =+"b";
         break;
         
    case 'x' : 
        results=+ "c";
	    break;
	    }
    default:
      cout << "Coder broken." << endl;


or...

1
2
encoder[]={"z","y","x"};
decoder[]={"a","b","c"};


then use a loop

1
2
3
if (string==encoder[i]){
results=+decoder[i];
}


2nd, I'd ask myself if I really needed that header file.
Last edited on
Well this assignment uses automatic submission and grading and the header file is one thing it looks for so I can't really omit that.

I see how the large amount of if else statements isn't good practice but that was an example I saw so I just followed it and kept going.

So would changing the if else statements fix the blank output?
Topic archived. No new replies allowed.