encryption/decryption program

Hello,

I'm new to these boards, and to C++ so I look forward to learning something new. Anyhow I've been working on this coding for a rudementry encryption/decryption program for awhile now and I seem to be stuck. Here is the code...
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
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <istream>
using namespace std;

char getmenuselection (char m)
{
	cout << "Select 'E' to encrypt a file or select 'D' to decrypt a file. ";
	cin >> m;
    switch (m)
    {
       case 'e':
	   case 'E':   cout << "You have selected to encrypt a file... " << endl;
                 break;
       case 'd':
	   case 'D':   cout <<  "You have selected to decrypt a file... " << endl; 
				break;
	   case 'q':
	   case 'Q':   cout <<  "You have selected to quit..." << endl; 
				break;
       default: cout << "Invalid entry, please try again... ";                
				return getmenuselection (m);
}return (m);
}
void encrypt (char key[]) 
{
	for(int i = 0; i < 5; i++) key[i] += 2; 
}										
void decryptkey (char code[]) 
{
	for(int i = 0; i < 5; i--) code[i] -= 2;
}
int main()
{
	char menu;
	char codekey;
	char myname[128];
	menu=0;

	getmenuselection(menu);
	
		if (menu == 'e');
		{
		cout << "You choose to encrypt...";
		cout << "Enter original file name... ";
		cin >> myname;
		encrypt (myname);										// call to the function encrypt( )
		cout << "Encrypted string is: " << myname <<endl;
		return main();

		if (menu == 'd');

		cout << "You want to decrypt...";
		cout << "Enter encrypted file name... ";
		cin >> myname;
		decryptkey (myname);									// call to the function decrypt( )
		cout << "Decrypted string is: " << myname <<endl;
		return main();
		}
		if (menu == 'q');
		{
		cout << "Goodbye...";
		cin.ignore(2);
		return 0;
		}
cin.ignore(2);
return 0;				
}

My goal is to be able to have users select either decrypt or encrypt then have the program call on the specific function to do what the user wants. But for some reason no matter what the user inputs, it always defaults to the encryp function. What am I doing wrong here? Thank you in advance.
Last edited on
If you put semicolon right after an if statement it treats that as an empty statement and will do nothing if the condition is true. The code that comes after will not be part of the if statement so it will always run.
That makes sense. OK so I removed the semicolon after the if statement. Now my code looks like 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
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <istream>
using namespace std;

char getmenuselection (char m)
{
	cout << "Select 'E' to encrypt a file or select 'D' to decrypt a file. ";
	cin >> m;
    switch (m)
    {
       case 'e':
	   case 'E':   cout << "You have selected to encrypt a file... " << endl;
                 break;
       case 'd':
	   case 'D':   cout <<  "You have selected to decrypt a file... " << endl; 
				break;
	   case 'q':
	   case 'Q':   cout <<  "You have selected to quit..." << endl; 
				break;
       default: cout << "Invalid entry, please try again... ";                
				return getmenuselection (m);
}return (m);
}
void encrypt (char key[]) 
{
	for(int i = 0; i < 5; i++) key[i] += 2; 
}										
void decryptkey (char code[]) 
{
	for(int i = 0; i < 5; i--) code[i] -= 2;
}
int main()
{
	char menu;
	char codekey;
	char myname[128];
	menu=0;

	getmenuselection(menu);

		if (menu == 'e')
		{
		cout << "You choose to encrypt...";
		cout << "Enter original file name... ";
		cin >> myname;
		encrypt (myname);								// call to the function encrypt( )
		cout << "Encrypted string is: " << myname <<endl;
		return main();

		if (menu == 'd')

		cout << "You want to decrypt...";
		cout << "Enter original file name... ";
		cin >> myname;
		decryptkey (myname);							// call to the function decrypt( )
		cout << "Decrypted string is: " << myname <<endl;
		return main();
		}
		if (menu == 'q')
		{
		cout << "Goodbye...";
		cin.ignore(2);
		return 0;
		}
cin.ignore(2);
return 0;				
}


So now when I make a selection, it outputs the correct cout statement in the function but it no longer continues. Meaning now it wont even call upon the correct function...Grrrr so frustrating!
you are passing variable m in function char getmenuselection (char m) as pass by value.So the changes done for variable m will not reflect back to main function.

So after function call "getmenuselection(menu);" the value of menu is still zero.That is the reason why correct function is not being called.

You can either pass variable m as pass by reference
or you can get the value of menu in another variable .

Example:
char selectedMenu;
selectedMenu = getmenuselection(menu);

You should also correct the scope/braces of "if (menu == 'e')"
In current scenario if user selects option d , the control will come to first if, condition fails and it will not hit the check "if (menu == 'd')" as it is inside first if.

Ah that makes sense.... Here is what I did.

1
2
3
4
5
6
7
8
9
10
11
	
menu=getmenuselection(menu);

		if (menu == 'e')
		{
		cout << "Enter original file name... ";
		cin >> myname;
		decryptkey (myname);								// call to the function encrypt( )
		cout << "Encrypted string is: " << myname <<endl;
		return main();
		}


That seems to have did the trick. Now I just got to figure out how to tell the program to handle more than 5 characters. Thanks for everyones help thus far. I am glad I joined, should've done this sooner.
OK I'm back. Here is my newly formatted coding that I've been working on...

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

char getmenuselection (char m);
void encrypt (char key[]);
void decrypt (char code[]);


int main()
{
	ifstream input;
	ifstream in;
	ofstream out;
	char menu;
	char file [80];
	char codekey;
	char myname[128];
	menu=0;

	menu=getmenuselection(menu); //Asks user what function to use.

		if (menu == 'e' || menu == 'E') //Option to encrypt a file.
		{
		cout << "Enter original file name... ";
		input.open (file);
		cin >> file;
		encrypt (file);
		input.close();									
		cout << "Encrypted string is: " << file <<endl;
		return main();
		}
		if (menu == 'd'|| menu == 'D')//Option to decrypt a file.
		{
		cout << "Enter original file name... ";
		input.open (file);
		cin >> file;
		decrypt (file);
		input.close();									
		cout << "Decrypted string is: " << file <<endl;
		return main();
		}
		if (menu == 'q') //Option to end the program.
		{
		cout << "Goodbye...";
		cin.ignore(2);
		return 0;
		}
cin.ignore(2);
return 0;				
}
char getmenuselection (char m) //Menu selection function.
{
	cout << "Select 'E' to encrypt a file or select 'D' to decrypt a file. ";
	cin >> m;
    switch (m)
    {
       case 'e':
	   case 'E':   cout << "You have selected to encrypt a file... " << endl;
                 break;
       case 'd':
	   case 'D':   cout <<  "You have selected to decrypt a file... " << endl; 
				break;
	   case 'q':
	   case 'Q':   cout <<  "You have selected to quit..." << endl; 
				break;
       default: cout << "Invalid entry, please try again... ";                
				return getmenuselection (m);
}return (m);
}
void encrypt (char key[]) //encryption function.
{
	for(int i = 0; i < 5; i++) key[i] += 2; 
}										
void decrypt (char code[]) //decryption function.
{
	for(int i = 0; i < 5; i++) code[i] -= 2;
}


So earlier in this thread My goal was to get the program to take a char user input and output it after it got transformed in either the encrypt function or decrypt function. Now I want to try to take this one step further and have the program obtain the user input from a txt. file. I have no errors when I run this code but it does not act like its supposed to. What gives?
1
2
input.open (file);
cin >> file;

I think these two statements are in the wrong order.
Also, you need to read the file to encrypt or decrypt it's data.
Topic archived. No new replies allowed.