URGENT: Encryption/Decryption Program

I am making a C++ program for my college class due at the end of the week. The program is supposed to encrypt, decrypt, or exit the program. So I did all I could on my own, and then sought further help in the archives, but I'm still stuck. Please help me. Here is my 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>

using namespace std;

void Encryption()
{
	string fileloc, password, outfileloc; // Initializing strings
	cout << "\nEnter the location of the file you would like to encrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile.open (fileloc.c_str());	// Opens the input file
	cout << "\n\nEnter a four letter password for encryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores encryption password
	cout << "\n\nEnter a file name for the encrypted file.\n";
	getline (cin, outfileloc);	// Recieves OutputFile
	cin >> outfileloc; // Stores output file name and location
	outfile.open (outfileloc.c_str()); // Opens the output file
	while (!infile.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be encrypted
			infile.get(name); // Takes character from file
			name=name+password[i]; // Adds the part of the password
			name=name-password[0]; // Takes away the first letter of the password
			outfile<<name; //prints that character to the output file
		}
	}
	infile.close(); // Closes the input file
	outfile.close(); // Closes the output file
}
void Decryption()
{
	string fileloc, password; // Initializing strings
	cout << "\nEnter the location of the file you would like to decrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile2.open (fileloc.c_str());	// Opens differnt input file
	cout << "\n\nEnter the four letter password for decryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores decryption password
	while (!infile2.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be decrypted
			infile2.get(name); // Takes character from file
			name=name+password[0]; // Adds the first letter of the password again
			name=name-password[i]; // Takes away the proper letter from the password array
			cout<<name; // Displays decrypted character
		}	
	}
	infile2.close();	// Closes decrypted file
}
int main()
{
	int choice;
	cout<<"Welcome to Crypt, an encrypting and decrypting program.\nEnter a menu option (1/2/3) to follow that command.\n\n";
	cout<<"\n\n 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
	cin>>choice;
	while (choice!=1 || choice!=2 || choice!=3)
	{
		cout<<"\n\nError! Please enter an appropriate menu choice. ( '1' '2' or '3' )";
		cout<<"\n\n 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
		cin>>choice;
	}
	if (choice=1)
	{
		Encryption()
	}
	else if (choice=2)
	{
		Decryption()
	}
	else if (choice=3)
	{
		return 0;
	}
	cout<<"\n\n";
	return 0;
}

Last edited on
So.....??? Where do you need help? ALWAYS be explicit: If you get error messages, post them; if your program doesn't behave as expected, explain the current behavior and the desired behavior.

See http://www.cplusplus.com/forum/beginner/1/ .
My program won't compile due to the errors listed below.
Please see my original post for desired behavior. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
Compiling...
Hello.cpp
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(17) : error C2065: 'infile' : undeclared identifier
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(17) : error C2228: left of '.open' must have class/struct/union: type is ''unknown-type''
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(24) : error C2065: 'outfile' : undeclared identifier
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(24) : error C2228: left of '.open' must have class/struct/union: type is ''unknown-type''
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(25) : error C2065: 'infile' : undeclared identifier
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(25) : error C2228: left of '.good' must have class/struct/union: type is ''unknown-type''
c:\users\brandon\documents\downloads\c++\hello_world\hello.cpp(25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Build log was saved at "file://c:\Users\Brandon\Documents\Downloads\C++\Hello_World\Debug\BuildLog.htm"
Hello_World - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
Did you actually read the errors and look at the lines they are pointing at? They are quite self explanatory IMO.
Agreed. I'll just say this: The errors C2228 are a consequence of the C2065 ones. Correct C2065 and be done with the C2228 too.
Honestly, I looked at all the errors, and I don't know what else to do. Please help me!
You have neither 'infile' nor 'outfile' declared.

There must be something like
1
2
fstream infile; // Or whatever stream you want
fstream outfile; // Or whatever stream you want 
before line 17

If you declare that within a function then you don't need something like 'infile2' in another function
Now, when I run my program, every menu option starts the Encryption function, but choice '2' should start the Decryption function, and choice '3' should print the "goodbye" message to the screen and quit the program.

Also, the encryption doesn't even work. It ends normally, but no encrypted file is created. The command prompt is shown in the following image:
http://mymultimedii.webs.com/Menu-Problem.jpg


Please help me as soon as possible. Thank you so much!

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
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>

using namespace std;

void Encryption()
{
	fstream infile;
	fstream outfile;
	string fileloc, password, outfileloc; // Initializing strings
	cout << "\nEnter the location of the file you would like to encrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile.open (fileloc.c_str());	// Opens the input file
	cout << "\n\nEnter a four letter password for encryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores encryption password
	cout << "\n\nEnter a file name for the encrypted file.\n";
	getline (cin, outfileloc);	// Recieves OutputFile
	cin >> outfileloc; // Stores output file name and location
	outfile.open (outfileloc.c_str()); // Opens the output file
	while (!infile.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be encrypted
			infile.get(name); // Takes character from file
			name=name+password[i]; // Adds the part of the password
			name=name-password[0]; // Takes away the first letter of the password
			outfile<<name; //prints that character to the output file
		}
	}
	infile.close(); // Closes the input file
	outfile.close(); // Closes the output file
}
void Decryption()
{
	fstream infile2;
	fstream outfile;
	string fileloc, password; // Initializing strings
	cout << "\nEnter the location of the file you would like to decrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile2.open (fileloc.c_str());	// Opens differnt input file
	cout << "\n\nEnter the four letter password for decryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores decryption password
	while (!infile2.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be decrypted
			infile2.get(name); // Takes character from file
			name=name+password[0]; // Adds the first letter of the password again
			name=name-password[i]; // Takes away the proper letter from the password array
			cout<<name; // Displays decrypted character
		}	
	}
	infile2.close();	// Closes decrypted file
}
int main()
{
	int choice;
	cout<<"\nWelcome to Crypt, an encrypting and decrypting program.\nEnter a menu option (1/2/3) to follow that command.\n\n";
	cout<<" 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
	cin>>choice;
	while (choice!=1 && choice!=2 && choice!=3)
	{
		cout<<"\n\nError! Please enter an appropriate menu choice. ( '1' '2' or '3' )";
		cout<<"\n\n 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
		cin>>choice;
	}
	if (choice=1)
	{
		Encryption();
	}
	else if (choice=2)
	{
		Decryption();
	}
	else if (choice=3)
	{
		cout<<"Thank you for using Crypt, have a nice day!";
	}
	cout<<"\n\n";
	return 0;
}
Last edited on
line 78,82, 86 are assignments (you set choice to 1 which is always true hence always 'Encryption()' is called.

line 27 and 53 will loop as long as it's not(!) good.

line 17 and 18 (and alike) retrieves the string twice (1. as line 2. as word)
Thanks coder777. But I don't understand your 3rd correction. Please clarify.

line 17 and 18 (and alike) retrieves the string twice (1. as line 2. as word)


Here is my new current source code (with your first two fixes taken into account):

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
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>

using namespace std;

void Encryption()
{
	fstream infile;
	fstream outfile;
	string fileloc, password, outfileloc; // Initializing strings
	cout << "\nEnter the location of the file you would like to encrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile.open (fileloc.c_str());	// Opens the input file
	cout << "\n\nEnter a four letter password for encryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores encryption password
	cout << "\n\nEnter a file name for the encrypted file.\n";
	getline (cin, outfileloc); // Recieves OutputFile
	cin >> outfileloc; // Stores output file name and location
	outfile.open (outfileloc.c_str()); // Opens the output file
	while (infile.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be encrypted
			infile.get(name); // Takes character from file
			name=name+password[i]; // Adds the part of the password
			name=name-password[0]; // Takes away the first letter of the password
			outfile<<name; //prints that character to the output file
		}
	}
	infile.close(); // Closes the input file
	outfile.close(); // Closes the output file
}
void Decryption()
{
	fstream infile2;
	fstream outfile;
	string fileloc, password; // Initializing strings
	cout << "\nEnter the location of the file you would like to decrypt.\n";
	getline (cin, fileloc);	// Recieves InputFile
	cin >> fileloc; // Stores input file name and location
	infile2.open (fileloc.c_str());	// Opens differnt input file
	cout << "\n\nEnter the four letter password for decryption.\n";
	getline (cin,password);	// Recieves Password
	cin >> password; // Stores decryption password
	while (infile2.good()) // While file is good (bad at eof)
	{
		for (int i=0 ;i<4 ; i++) // Positions of password
		{
			char name; // Recieves file character to be decrypted
			infile2.get(name); // Takes character from file
			name=name+password[0]; // Adds the first letter of the password again
			name=name-password[i]; // Takes away the proper letter from the password array
			cout<<name; // Displays decrypted character
		}	
	}
	infile2.close();	// Closes decrypted file
}
int main()
{
	int choice;
	cout<<"\nWelcome to Crypt, an encrypting and decrypting program.\nEnter a menu option (1/2/3) to follow that command.\n\n";
	cout<<" 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
	cin>>choice;
	while (choice!=1 && choice!=2 && choice!=3)
	{
		cout<<"\n\nError! Please enter an appropriate menu choice. ( '1' '2' or '3' )";
		cout<<"\n\n 1: Encrypt a file\n 2: Decrypt a file\n 3: Exit the program\n\nMenu Choice: ";
		cin>>choice;
	}
	if (choice==1)
	{
		Encryption();
	}
	else if (choice==2)
	{
		Decryption();
	}
	else if (choice==3)
	{
		cout<<"Thank you for using Crypt, have a nice day!";
	}
	cout<<"\n\n";
	return 0;
}
Thanks coder777. But I don't understand your 3rd correction. Please clarify.
The point is that cin>>... leaves an end of line ('\n') in the stream. So getline() on line 17 will get an empty line and on line 18 you will get the real value (if you cout 'fileloc' after line 17 you'll see that).

In other words: you can remove all getline()'s. They're unnecessary
Topic archived. No new replies allowed.