Encrypting for a school

So, I'm new, to this forum and to c++. Second, this is for a class, so I really don't want the answer, as I would like to learn the coding.
1
2
3
4
5
6
7
8
9
10
11
	if (m == 'e' || m == 'E' ) 
			{
				encryptionChoice(key[]); // here is the problem
			}
	 
		else if ( m == 'D' || m == 'd')
			{
				
				decryptionChoice(key[]); // and here!!
			}
	}

(hopefully this is formatted correctly)
These are both in the function prototype, in the beginning of my program and look like this
1
2
void encryptionChoice (char []);
void decryptionChoice (char []);

It is part of a do statement after a menu choice. I have the "key[]" initialized.
Finally a question, do I need a value in here in the prototype, or can I get away with it being blank, even though I declared values?...
well if the functions take a char *

1
2
3
4
5
6
7
8
9
10
11
void encrypt(char c[]){
	do
	{
		std::cout << *c;

	}while(++c);
}

// same as

void encrypt(char* c){...}


then you can just send

 
encryptionChoice(key);


when variables are declared with [] the variable just gets a pointer to the base address

so "key" is a basically a char pointer
Last edited on
That makes sense. I am having a little issue writing this one for class. Thank you.
So.. I got my code here, It's an encryption/decryption simple program. I don't understand what I did wrong. Please no code corrections just the direction of correction. I know I have some redundant or not needed code here. It is still a work in progress.
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
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>

using namespace std;

// function prototypes

void displayMenu();
char getMenuChoice (void);
void encryptionChoice (char []);
void decryptionChoice (char []);
char key (char);
char encrypt (char);
char decrypt (char);

//main body function

int main()
{

char code;
char m;	

	do
	{
		displayMenu();
		m = getMenuChoice();
	
		if (m == 'e' || m == 'E' ) 
			{
			encrypt(code); 
			}
	 
		else if ( m == 'D' || m == 'd')
			{
				
			decrypt(code); 
			}
	}

	while (m != 'q' || m != 'Q');

}

// functions

void displayMenu ()
{
cout << "\n\tThe ENCRYPTION / DECRYPTION PROGRAM!\n"
	<< "\tPress (E), then ENTER, To start ENCRYPTING \n"
	<< "\tPress (D), then ENTER, To start DECRYPTING \n"
	<< "\tTo QUIT, Press (Q), then ENTER: \n\n";
}

char getMenuChoice () //Menu selection function.
{
	char m;

	ifstream in;
	ofstream out;
	string infile;
	string outfile;


	cout << endl;
	cin >> m;
    
	switch (m)
    {
       case 'e':
	   case 'E':  
		   {
	            return m;
		    break;
		   }	
             
       case 'd':
	   case 'D':   
		   {
		    return m;
		    break;
		   }

	   case 'q':
	   case 'Q': 
		   {
		   exit(0);
		   }

       default: 
		{   
		cout << "\t--------------------------------------\n";
		cout << "\n\t--- Invalid input: Please restart. ---\n\n";
		cout << "\t--------------------------------------\n";
		cout << endl;
		displayMenu();
		getMenuChoice();
		break;
	        }
	}

}

void encryptionChoice (char key[]) //encryption file function.
{
	ifstream in;
	ofstream out;
	string infile;
	string outfile;
	char code;
	
		cout << "Enter the input file name to be ENCRYPTED: ";
		cin >> infile;
		cout << endl;
		infile = infile + ".txt";
		in.open (infile);
		cout << "Enter the output file name to write to: ";
		cin >> outfile;
		cout << endl;
		outfile = outfile + ".txt";
		out.open (outfile);
		encrypt(code);
		cout << "The ENCRYPTION IS: " << infile <<endl;
		in.close();	
		out.close();
}	

void decryptionChoice (char key[]) //decryption file function.
{
	ifstream in;
	ofstream out;
	string infile;
	string outfile;
	char code;

		cout << "Enter the input file name to be DECRYPTED: ";
		cin >> infile;
		cout << endl;
		infile = infile + ".txt";
		in.open (infile);
		cout << "Enter the output file name to write to: ";
		cin >> outfile;
		cout << endl;
		outfile = outfile + ".txt";
		out.open (outfile);
		decrypt(code);
		cout << "The DECRYPTION IS: " << outfile <<endl;
		in.close();	
}

char encrypt (char code) // encryption key
{
	if ((code >= 'a' && code < 'z') || (code >= 'A' && code < 'Z'))
	{	
		return code+1;
	}

	if (code == 'z')
	{	
		return 'a';
	}
	if (code == 'Z')
	{
		return 'A';
	}
	return code;
}

char decrypt (char code) // decryption key
{
	if ((code >= 'a' && code <= 'z') || (code >= 'A' && code <= 'Z'))
	{	
		return code-1;
	}

	if (code == 'z')
	{	
		return 'a';
	}
	if (code == 'Z')
	{
		return 'A';
	}
	return code;
}
I will just comment in the code instead of correcting for you...

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

        //Use a while statement around this switch that way if a return is never hit, it just loops back here
        //This should look similar to the loop you have in main! for a simple menu, I believe that is better than recursion
        //call getMenu()
	switch (m)
    {
       case 'e':
	   case 'E':  
		   {
	            return m;
		    break;
		   }	
             
       case 'd':
	   case 'D':   
		   {
		    return m;
		    break;
		   }

	   case 'q':
	   case 'Q': 
		   {
		   exit(0);
		   }

       default: 
		{   
		cout << "\t--------------------------------------\n";
		cout << "\n\t--- Invalid input: Please restart. ---\n\n";
		cout << "\t--------------------------------------\n";
		cout << endl;
		displayMenu();       //Correct
		getMenuChoice(); //not here, this makes it recursive,  that can work but is unnecessary
		break;
	        }
	}



next

you declare these as such
1
2
void encryptionChoice (char []);
void decryptionChoice (char []);


but then define them as
1
2
void encryptionChoice (char code);
void decryptionChoice (char code);


the declaration and definition should match. Currently you are sending it a single char but declaring it to be an array of char.

Also, your functions are returning a value but that goes nowhere.

http://www.cplusplus.com/doc/tutorial/functions2/

but I will also explain, the way you have it, you need something to hold the returned value. I am going to assume you just want to work on a single char.

1
2
3
code = encrypt(code);
//
code = decrypt(code);


that way code is set to the returned value.

If you want to pass by reference, which means the parameter that you send in can be modified, then you need to
1
2
3
//declare
void encrypt(char& code);
void decrypt(char& code);


then in your definitions take out the return statements and just change the value of "code" directly.
Last edited on
Sorry for the slow reply, I appreciate you help. Thank you
Topic archived. No new replies allowed.