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...
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <istream>
usingnamespace 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.
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.
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <istream>
usingnamespace 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 .
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.
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.
#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
usingnamespace 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?