nclude    #include  #include    using namespace std;  class Caesar    {        public: void encrypt(char *inp,char *out,int key);                void decrypt(char *inp,char *out,int key);                void readText(char *inp);               void encrypt1();                void decrypt1();  };    void Caesar::encrypt(char *inp,char *out,int key)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

 #include<iostream>  
 #include<string>
 #include<fstream>  
 using namespace std;
 class Caesar  
 {  
     public: void encrypt(char *inp,char *out,int key);  
             void decrypt(char *inp,char *out,int key);  
             void readText(char *inp); 
             void encrypt1();  
             void decrypt1();
 };  
 void Caesar::encrypt(char *inp,char *out,int key)  
 {  
     ifstream input;  
     ofstream output;  
     char buf;  
     input.open(inp);  
     output.open(out);  
     buf=input.get();  
     while(!input.eof())  
     {  
         if(buf>='a'&&buf<='z')  
         {  
             buf-='a';  
             buf+=key;  
             buf%=26;  
             buf+='A';  
         }  
         output.put(buf);  
         buf=input.get();  
     }  
     input.close();  
     output.close();  
     readText(inp);  
     readText(out);  
 }  
  void encrypt1()  
 {  
   char message[100], ch;
 int i, key;
     ofstream myfile ("encrypt.dat");
 
     cout << "Enter a message to encrypt: ";
 cin>>message;
 cout << "\nEnter key: ";
 cin >> key;
 
 for(i = 0; message[i] != '\0'; ++i){
  ch = message[i];
  
  if(ch >= 'a' && ch <= 'z'){
   ch = ch + key;
   
   if(ch > 'z'){
    ch = ch - 'z' + 'a' - 1;
   }
   
   message[i] = ch;
  }
  else if(ch >= 'A' && ch <= 'Z'){
   ch = ch + key;
   
   if(ch > 'Z'){
    ch = ch - 'Z' + 'A' - 1;
   }
   
   message[i] = ch;
  }
  myfile << message[i] << " " ;
 }

 }  
  void decrypt1()  
 { 
     char message[100], ch;
 int i, key;
    ofstream myfile ("decrypt.txt");
    cout << "Enter a message to decrypt: ";
 cin>>message;
 cout << "Enter key: ";
 cin >> key;
 
 for(i = 0; message[i] != '\0'; ++i){
  ch = message[i];
  
  if(ch >= 'a' && ch <= 'z'){
   ch = ch - key;
   
   if(ch < 'a'){
    ch = ch + 'z' - 'a' + 1;
   }
   
   message[i] = ch;
  }
  else if(ch >= 'A' && ch <= 'Z'){
   ch = ch - key;
   
   if(ch > 'a'){
    ch = ch + 'Z' - 'A' + 1;
   }
   
   message[i] = ch;
  }
   myfile << message[i] << " " ;
 }
 }
 
 void Caesar::decrypt(char *inp,char *out,int key)  
 {  
     ifstream input;  
     ofstream output;  
     char buf;  
     input.open(inp);  
     output.open(out);  
     buf=input.get();  
     while(!input.eof())  
     {  
         if(buf>='A'&&buf<='Z')  
         {  
             buf-='A';  
             buf+=26-key;  
             buf%=26;  
             buf+='a';  
         }  
         output.put(buf);  
         buf=input.get();  
     }  
     input.close();  
     output.close();  
     readText(inp);  
     readText(out);  
 }  
 void Caesar::readText(char *inp)  
 {  
     ifstream input;  
     char buf;  
     input.open(inp);  
     cout<<"\n\n <--- "<<inp<<" --->\n";  
     buf=input.get();  
     while(!input.eof())  
     {  
         cout<<buf;  
         buf=input.get();  
     }  
     input.close();  
 }  
 int main()  
 {  
     Caesar a;  
     int choice,key,ch;  
     char inp[30],out[30];  
     cout<<"\n\n 1. using file\n 2. Using keyboard input\n\n Select choice(1 or 2): "; 
     cin>>ch;
     if(ch==1){
         cout<<"\n Enter input file: ";  
         cin>>inp;  
         cout<<"\n Enter output file: ";  
         cin>>out;  
         cout<<"\n Enter key: ";  
         cin>>key;  
         
       cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n 3. exit.Select choice(1 or 2. Press 0 for exit): ";  
       cin>>choice;  
       do{
         switch(choice){
        case 1:
         
            a.encrypt(inp,out,key); 
            break;
          
        case 2: 
           
             a.decrypt(inp,out,key);  
             break;
        case 3:
        exit(0);
            
        default:  cout<<"\n\n Unknown choice";  
        }
        }while(choice!=0);
    }
    else if(ch==2)
    {
       cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): ";  
       cin>>choice;  
       do{
         switch(choice){
        case 1:
         
            encrypt1(); 
            break;
          
        case 2: 
           
             decrypt1();  
             break;
            
        default:  cout<<"\n\n Unknown choice";  
        }
        }while(choice!=0);
    }
     
 }

Question

The code above has an infinite loop when it is being executed in code blocks please fix it

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY