Finally , ı finished my simple program.
I will learn very very much thing from you
And ı will ask many many questions after this time:)
So

I will wait your suggestions about my program

#include <iostream>
using namespace std;
void encyrpt(void);                                                             // Encyrpt function prototype
void decyrpt(void);                                                             // Decyrpt function prototype
void menu (void)                                                                // this function provide to be reached to menu
{
     char a;
     
     cout<<"(E/e) Encryption"<<endl;
     cout<<"(D/d) Decryption"<<endl;
     cout<< "(Q/q) Quit"<<endl;
     
     cout<<"Enter operation code      :";
     do
     {
     cin>>a;
     
     
     if ( a!='e' && a!='d' && a!='E' && a!='D')                                 // with respect to election , program will be redirecting encyrpt ot decyrpt
     cout<<"\n you entered an invalid operation code"<<endl;
     else if ( a=='e' || a=='E')
     encyrpt();
     else if (a=='d' || a=='D')
     decyrpt();
     }
     while (a!='q' && a!='Q');
     
     return;                                                                    //  when  key is q , it should quit program
     }

     void encyrpt (void)                                                        // this is encyprt function
{
    int num,a,b,c,d,e,f;
    char g,h;
    cout<<"the 4-digit integer to be encrypted"<<endl;
    
    
   
   do {                                                                         // thanks to do while, if user enters wrong number, we can warn him 
   
   cin>>num;                                                                    // user will enter a integer with four number
    if (num<=999 || num>=10000)                       
    cout<<" You entered an invalid integer!"<<endl;
    
    
    else {
    a=num/1000;
    b=num%1000;
    c=b/100;                                                                    // this is encyription formula
    d=b%100;
    e=d/10;
    f=d%10;
    cout<<"Encrypted integer         " <<(a+f)%10<<(c+f)%10<<(e+f)%10<<(f+f)%10<<endl;
    cout<<"Do you want to encrypt another integer? (y/n) :"<<endl;              // we ask only two selection y and n               
    }
    }
    while (num<=999 || num>=10000);                                             //we want to be entered integer with four number 

 


    do 
    {                                                                           // thanks to do while, when user entered wrong character, computer will warn user and it has loop
    cin>>h;                                                          
    if(h=='y')                                                     
    encyrpt();
    else if (h!='y' && h!='n')
    cout<<"You entered an invalid character!"<<endl;                                          
    else                                                           
    menu();     
    } 
    while(h!='y' || h!='n');                                                    //durint not being h is y or n , do while will loop


}

void decyrpt(void)                                                              // decyrpt function
{    
     int num,a,b,c,d,e,f,m,n,r,k;
     char h;

     cout<<"the 4-digit integer to be decrypted"<<endl;                         // ı use do whilee loop because when user entered true number , loop must be stop
     do
     {
     cin>>num;
     if (num<=999 || num>=10000)                                                // if number is entered wrong , user should be warn
     cout<<" You entered an invalid integer!"<<endl;                            // thanks to do while, if user enters wrong number, we can warn him 
     
     else{                                                                      // if user enter true number, decyripted number is calculated , and computer asks user whether or not he want to use decyrption function again.
     a=num/1000;
     b=num%1000;
     c=b/100;
     d=b%100;
     e=d/10;
     f=d%10;
     
     k=(10+f)/2;
     r=( (10+e-f)%10);
     n=( (10+c-f)%10);
     m=( (10+a-f)%10);
     
     cout<<"Decrypted integer is    "<< m<<n<<r<<k<<endl;
     cout<<"Do you want to encrypt another integer? (y/n) :"<<endl;
     }
     }
     while (num<=999 || num>=10000);                                            //while entered number is not true , loop must be done again
     
     
     
     do                                                                         // after calculation are displayed , computer asks to you whether or not you want to do again decyrption process     
    {
    cin>>h;
    if(h=='y')
    decyrpt();
    else if (h!='n' && h!='y')
    cout<<"You entered an invalid character!"<<endl;
    else
    menu();
    } 
    while(h!='y' || h!='n');                                                    //until user enters true character , you should ask for right character
    }
     
     
     
     int main(void)                                                             
     {
     menu();                                                                    //as soon as program runs , to ask user to menu questions , menu function runs.        
     return 0;
     }

Recommended Answers

All 3 Replies

In void encyrpt (void) you define g but don't use it. Might as well kill it.

I suggest removing all cin and cout statements from encrypt and decrypt. Those functions should be used to encrypt and decrypt. It should be other functions' job to provide encrypt and decrypt with data and decide what to do with it. Perhaps I would like to use your decrypt and encrypt functions, but I do not need to ask the user for data or display anything. I could not use your functions.

I suggest removing all cin and cout statements from encrypt and decrypt. Those functions should be used to encrypt and decrypt. It should be other functions' job to provide encrypt and decrypt with data and decide what to do with it. Perhaps I would like to use your decrypt and encrypt functions, but I do not need to ask the user for data or display anything. I could not use your functions.

Yes. you are completely right. may be ı can need this functions another program. this is good suggestion for me .
After this . my functions will be used another programs:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.