Question

In: Computer Science

Please write this code in C++, also if you could please bold the names of the...

Please write this code in C++, also if you could please bold the names of the input files and provide comments for each choice.

For this part, the program gives the user 4 choices for encrypting (or decrypting) the first character of a file. Non-lowercase characters are simply echoed. The encryption is only performed on lowercase characters.

  • If c is char variable, then islower(c) will return true if c contains an lowercase character, false otherwise
  • To read a single character from a file (let inFile be the name of your ifstream), you can use: inFile.get(c);
    • This will read one character, even the whitespace characters
  • Choice 1 – No encryption, echo the character from the file
  • Choice 2 – Encrypt by shifting. For example, shifting by 3 characters would change an ‘a’ to a ‘d’ because that is 3 letters later. If you reach the end of the alphabet then you need to roll over. For example ‘z’ plus 3 is ‘c’.
    • NOTE: The process of converting the ‘z’ to a ‘c’ should NOT need the use of an if, switch, or loop statement.
    • For this week, you may use an if or switch if you need, but next week you’ll have to do it without
  • Choice 3 – This is the opposite of choice 2. Instead of moving 3 letters forward, it will move 3 letters backwards. A ‘d’ will become ‘a’.
    • Like choice 2, the shifting can be accomplished without if’s, switch’s, and loop’s.
  • Choice 4 – This will calculate a hash value. You sum the ASCII values of all of the characters and at the end of the file print the last 2 digits of the sum. For example, “abc” is 94 because ‘a’ is 97, ‘b’ is 98, ‘c’ is 99, which has a sum of 294.
    • Remember, this week we are only reading a single character
  • Your program should prompt the user for:
    • The encryption type
    • The name of a file
    • For this week, the prompt will be very short, just a ?
  • Your program should output the encrypted characters (or a hash value).

This is the input files named file1.txt and file2.txt

  • file1.txt has the following contents:
yippee
  • file2.txt has the following contents:
A-bba cc
xyyz

This is what the user is typing it's in bold, and the output is under it.

Sample Run #1 (bold, underlined text is what the user types):

? 1 file1.txt

y

Sample Run #2 (bold, underlined text is what the user types):

? 2 file1.txt

b

Sample Run #3 (bold, underlined text is what the user types):

? 3 file1.txt

v

Sample Run #4 (bold, underlined text is what the user types):

? 4 file1.txt

21

Sample Run #5 (bold, underlined text is what the user types):

? 1 file2.txt

A

Sample Run #6 (bold, underlined text is what the user types):

? 2 file2.txt

A

Sample Run #7 (bold, underlined text is what the user types):

? 4 file2.txt

0

Solutions

Expert Solution

Code:

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


int main()
{
   int choice; // stores user choice of encryption
   string fileName; // stores filename
   // prompting user
   cout << "? ";
   cin >> choice >> fileName;

   // opens the file
   ifstream inFile;
   inFile.open(fileName.c_str());

   char c; // character from file
   inFile.get(c); // reading first character from file
   bool lower = islower(c); // if it's lowercase
   char c_new;

   if (choice == 1) // if user chooses print character
   {
       // printing first character
       cout << endl << c << endl;
   }
   else if (choice == 2 && lower) // if user chooses to shift forward and character is lowercase
   {
       c_new = (((int)c - 97) + 3)%26 + 97;
       cout << endl << c_new << endl;
   }
   else if (choice == 3 && lower) // if user chooses to shift backward and character is lowercase
   {
       if (c > 'c')
       {
           c_new = (((int)c - 97) - 3)%26 + 97;
       }
       else if (c == 'a')
       {
           c_new = 'x';
       }
       else if (c == 'b')
       {
           c_new = 'y';
       }
       else
       {
           c_new = 'z';
       }
       cout << endl << c_new << endl;
   }
   else if (choice == 4 && lower) // if user chooses to print last two digits of ascii value
   {
       cout << endl << (int)c%100 << endl;
   }
   else if (choice == 4 && !lower) // if character is upper case
   {
       cout << endl << 0 << endl;
   }
   else // if character is upper case
   {
       cout << endl << c << endl;
   }

   // closing file
   inFile.close();

   return 0;
}

OUTPUT:


Related Solutions

Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
Could you please also give me a code that does not use the string split method...
Could you please also give me a code that does not use the string split method but instead gives an input and output file that you have to compile using "type output.txt" in java. This is the code: Record.java public class Record { private String stateCode, districCode, districtName; private int totalPopulation, childPopulation, childPovertyPopulation; private String miscStats; public Record(String stateCode, String districCode, String districtName, int totalPopulation, int childPopulation, int childPovertyPopulation, String miscStats) { this.stateCode = stateCode; this.districCode = districCode; this.districtName =...
Could you please write an article on Software Engineering Trends in about 300 words? Please, also...
Could you please write an article on Software Engineering Trends in about 300 words? Please, also provide appropriate references. Please, do not plagiarize your answer.
Could you please write an article on Software Engineering Trends in about 300 words? Please, also...
Could you please write an article on Software Engineering Trends in about 300 words? Please, also provide appropriate references.
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT