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:
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
usingnamespace 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()
}
elseif (choice=2)
{
Decryption()
}
elseif (choice=3)
{
return 0;
}
cout<<"\n\n";
return 0;
}
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.
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 ==========
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:
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
usingnamespace 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();
}
elseif (choice=2)
{
Decryption();
}
elseif (choice=3)
{
cout<<"Thank you for using Crypt, have a nice day!";
}
cout<<"\n\n";
return 0;
}
#include<iostream>
#include<string>
#include<fstream>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<iomanip>
usingnamespace 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();
}
elseif (choice==2)
{
Decryption();
}
elseif (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