Friday, July 31, 2009

I need help with a c++ program please?

Ok I need to write a program that encrypts and decrypts entered.


It also has to take into account a "shift" value that changes the encryption/decryption output.


like say the value for k is 1, if you enter a shift value of 7 and ask for the program to encrypt k it should print out 8





Here is the program:





#include%26lt;iostream%26gt;


using namespace std;





int menu(){


cout%26lt;%26lt;"Press 1 to encrypt, 2 to decrypt, 3 to quit)"%26lt;%26lt;endl;


int x;


cin%26gt;%26gt;x;


return x;





}


void encrypt(char){


char letter;





cout%26lt;%26lt;"Enter a message to encrypt (! to quit)"%26lt;%26lt;endl;


cin%26gt;%26gt;letter;


while (letter!='!'){


if (letter=='k') cout%26lt;%26lt;"1 ";


followed by 26 more if statements for other letters





cin%26gt;%26gt;letter;


}


return;


}











void decrypt(){


cout%26lt;%26lt;"Enter a message to decrypt (0 to quit)"%26lt;%26lt;endl;


int code;


cin%26gt;%26gt;code;


while (code!=0){


if (code==1) cout%26lt;%26lt;"k";


Followed by 26 more if statements





(will continue in additional details)

I need help with a c++ program please?
looks like you haven't even used the shift value in your encrypt function. you can't use the shift variable defined in main() because it's out of scope in encrypt()





just change the encrypt function to encrypt(int s) -you don't need a char there- then before printing out add s to 1 like this:


if(letter=='k')


{


cout%26lt;%26lt;s+1


}


now you have both problems solved.


good luck and I hope this helped.





ps: since you have 26 options I suggest you use a case construct instead of so many if statements.
Reply:This scares me!!! I took C++ a fews years back and out teacher made us code everything in notebad. I hated that class, good luck because I don't have clue of what is wrong.
Reply:1) u have not included shift value in ur program.u can check it out.


2) to make functions encrypt(int),just replace the prototype and definition arguement type to int and also store them in int too in the function.

augustifolia

No comments:

Post a Comment