Question

In: Computer Science

C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...

C++
Given Code:

#include <iostream>
#include <string>
using namespace std;


int main()
{
//declare variables to store user input

bool cont = true;

//implement a loop so that it will continue asking until the user provides a positive integer
// the following provides ONLY part of the loop body, which you should complete
{
cout <<"How many words are in your message? \n";
cout <<"Enter value: ";

// get user input integer here
  
cout <<"\nInvalid value. Please Re-enter a number of positive value\n";

}

//declare a dynamic array of string type with the specified number of elements

  
  
//
while(cont)
{
cout <<"\nSelect one of these options: (1) Get Message (2) Encrypt (3) Print (4) Quit";
cout <<"\nSelection: ";

// obtain user input option (an integer)

// based on the input the program will perform one of the following operations using switch statement
switch(selection)
{
case 1://Get Message

// Get the specified number of words from the user

break;

case 2: //Encrypt

// Based on the shifting encryption strategy described above, encrypt the individual words
// Be careful about the difference between the lower case letters and upper case letters
  
break;

case 3: //Print

// Print out the encrypted words

break;

case 4:
cont = false;
break;

default:
break;
}
}
  
// Remember to release the memory you allocate above!!
  
return 0;

}


Encryption using Pointers and Dynamic Arrays

Objective:

Agent James Vond, one of our secret agents, Agent 008, has recently been captured in the Caribbean. We have been able to establish contact with him, but need help sending him messages in secret to let him know help is on the way. We believe we can send him messages using a Caesar Cipher, but need your help encoding it. Agent Vond, we task you with encrypting the message and helping Agent 008 escape the Caribbean

Sincerely, M.

Assignment:

The Caesar Cipher technique is one of the earliest and simplest method of encryption. It’s a substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B would become C, and so on.

The encryption equation is E(x) = (x + n) % 26.

The x = the numeric letter value, n = number of shifts, and (% 26) is so that whatever value your result is, it will stay within the alphabet range (0-25).

However, since we will be looking at characters in strings, you need to implement ASCII code so that your equation will look more like:

letter = ( ( (letter - 'A') + shift ) % 26 + 'A')) if it is upper case

Or

letter = ( ( (letter - 'a') + shift ) % 26 + 'a' ) ) if it is lower case.

You subtract 'A' and 'a' in order to get the value of the letter. In ASCII code, letters are assigned values. For example, 'B' is 66. If I subtract the value of 'A' (65) then I get 1. This helps me encode because I can get the values 0-25 for each letter.

Your assignment is to implement a dynamic string array in order to store a message of variable length, and then a pointer to iterate through each letter of the string in order to encrypt it using the equations given above.

You will need to ask the user for the amount of words in the message and then create your dynamic array that way. If the amount of words is a negative number or equal to zero you should output to the screen "Invalid value. Please Re-enter a number of positive value" until they have the correct input.

Get Message

Use cin to get each word the user wishes to put into the message.

After they have input all the strings required, ask for the number of letter shifts they would like to do

Encrypt

Encrypt each letter in each string using the equations provided above if the getMessage pointer isn't null (aka if Get Message option has already been called)

Print Message

Print out the encrypted or unencrypted message

Quit

Simply quits out of the loop

Input:

You many assume that the input to be encrypted will only consist of capital and lower letters, and no other symbols such as white spaces, numbers, or special characters will have to be encrypted.

In order to choose the different options you will take an integer value as input. 1 is for Get Message, 2 is for Encrypt, 3 is for Print, 4 is for Quit

Requirements:

  • Use a dynamic array to store a message of variable length (not using dynamic array will receive 50% penalty)

  • Use a pointer to iterate through each string and encrypt it

  • Do not allow for number of letter shifts to be > 26 (Must be in the range of 0-25 aka the range of 26) [Ex: If I give you 27, your shift should end up being 1] [Hint: %]

  • Do not expect int to hold all possible values when I input the value of shift, test very large numbers

  • Print out your message using pointer arithmetic (ex: *p; p++;)

  • DO NOT iterate through your pointer like an array when printing!!

Solutions

Expert Solution

//############ PGM START #####################################

#include <iostream>
#include <string>
#include<sstream>
using namespace std;


int main()
{
   //declare variables to store user input
   int n;
   int selection;
   int shift;
   int i,j;
   string value="";
  
   bool cont = true;

   cout <<"How many words are in your message? ";
   cout <<"Enter value: ";

       // get user input integer here
   cin>>n;
   while(n<=0){
       cout <<" Invalid value. Please Re-enter a number of positive value ";
       getline(cin,value);
       n=stoi(value);
   }
   cin.ignore();

   string *msg=new string[n];
   while(cont){
       cout <<" Select one of these options: (1) Get Message (2) Encrypt (3) Print (4) Quit";
       cout <<" Selection: ";

       cin>>selection;
       cin.ignore();
       // based on the input the program will perform one of the following operations using switch statement
       switch(selection){
           case 1://Get Message
                   cout<<"Enter the msg: ";
                   i=0;
                   while(i<n){
                       cin>>*(msg+i);
                       i++;
                   }
                   cin.ignore();
                   // Get the specified number of words from the user
                   cout<<"Enter the number of letter shifts: ";
                   cin>>shift;
                   cin.ignore();  
                   break;

           case 2: //Encrypt
                   // Based on the shifting encryption strategy described above, encrypt the individual words
                   // Be careful about the difference between the lower case letters and upper case letters

                  
                   if(i>0){
                       i=0;
                       while(i<n){
                           string word=*(msg+i);
                           j=0;
                           while(j<word.length()){
                               char letter=word[j];
                               if(letter>='A' && letter<='Z'){
                                   word[j]= ( ( (letter - 'A') + shift ) % 26 + 'A');
                               }else{
                                   word[j]= ( ( (letter - 'a') + shift ) % 26 + 'a');
                               }
                               j++;
                           }
                           *(msg+i)=word;
                           i++;
                       }
                   }else{
                       cout<<" No msg entered!!!!! ";
                   }
                   break;

           case 3: //Print
                   // Print out the encrypted words
                   if(i>0){
                       i=0;
                       cout<<" Encrypted msg: ";
                       while(i<n){
                           cout<<*(msg+i)+" ";
                           i++;
                       }
                       cout<<" ";
                      
                   }else{
                       cout<<" No msg entered!!!!! ";
                   }
                   break;

           case 4:   cont = false;
                   cout<<" Exiting!!!!! ";
                   break;
          
           default:cout<<" Wrong selection!!!!!!!! ";
                   break;
       }  
  
   }


    // Remember to release the memory you allocate above!!
    delete[] msg;
   return 0;

}

//################## PGM END ########################################

OUTPUT
#########


Related Solutions

#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat...
I want Algorithim of this c++ code #include<iostream> using namespace std; int main() { char repeat = 'y'; for (;repeat == 'y';){ char emplyeename[35]; float basic_Salary,EPF, Dearness_Allow, tax, Net_Salary , emplyee_id; cout << "Enter Basic Salary : "; cin >> basic_Salary; Dearness_Allow = 0.40 * basic_Salary; switch (01) {case 1: if (basic_Salary <= 2,20,00) EPF = 0; case 2: if (basic_Salary > 28000 && basic_Salary <= 60000) EPF = 0.08*basic_Salary; case 3: if (basic_Salary > 60000 && basic_Salary <= 200000)...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start,...
c++ #include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT