Question

In: Computer Science

​​​​​​C++ It may seem hard but the instructions are listed, it's all about loops For this...

​​​​​​C++

It may seem hard but the instructions are listed, it's all about loops

  • 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).
    • file1.txt has the following contents:
yippee
  • file2.txt has the following contents:
A-bba cc
xyyz

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

#include <bits/stdc++.h>
using namespace std;

int main(){
   int choice;
   ifstream i;
   string fileName;
   cout<<"? ";
   cin>>choice>>fileName;
   i.open(fileName);
   char c;
   if(choice==1){
       cout<<c;      
   }
   if(choice==2){
       i>>c;
       if(islower(c)){
           int x = c;
           x+=3;
           if(x>122){
               x-=26;
           }
           cout<<(char)x;
       }
       else{
           cout<<c;
       }
   }
   if(choice==3){
       i>>c;
       if(islower(c)){
           int x = c;
           x-=3;
           if(x<97){
               x+=26;
           }
           cout<<(char)x;
       }
       else{
           cout<<c;
       }
   }
   if(choice==4){
       int x=0;
       while(i>>c){
           x+=(int)c;
       }
       cout<<(x%100);
   }
   return 0;
}

The main idea of the code lies in the fact that typecasting a char value to int by writing the (int) before it returns the ascii value of the character.


Related Solutions

11. Read about how flexible for-loops are in C. Write two examples of "non-traditional" for-loops (ie,...
11. Read about how flexible for-loops are in C. Write two examples of "non-traditional" for-loops (ie, for-loops that don't look like for-loops you learned in introductory courses) and explain how they work. 12. Compare/contrast case/when in Ruby with a switch statement in another language of your choice. 13. What does the last keyword do in a loop in Perl? 14. Many contemporary PLs allow two kinds of comments: one in which delimiters are used on both ends (multiple-line comments) and...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A...
Write the basic JAVA code for the beginners and follow all the instructions listed 1. A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for e.g., 1980), except that it is not a leap year if it is also divisible by 100 (for e.g., 1900); however, it is a leap year if it is further divisible by 400 (for e.g., 2000). Write a program that prompts the...
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
THIS ASSIGNMENT MUST BE DONE IN C++ 17 AND MUST FOLLOW EACH GUIDELINE LISTED BELOW. INSTRUCTIONS...
THIS ASSIGNMENT MUST BE DONE IN C++ 17 AND MUST FOLLOW EACH GUIDELINE LISTED BELOW. INSTRUCTIONS START HERE: Our satisfied clients are back to ask us to implement another interactive dictionary. Our dictionary takes input from users and uses the input as search key to look up values associated with the key. Requirements: - Coding: No hard coding - Data Source: a text file, Data.CS.SFSU.txt - Data Structure: Use existing data structure(s) or create new data structure(s) to store our...
Make 2 hard programming (1 about pattern and 1 about anything) problem in c++ that can...
Make 2 hard programming (1 about pattern and 1 about anything) problem in c++ that can be solved in around 30 minute give the problem + the answer please i need for learning c++
Decisions about alpha level may be different, especially as it relates from hard sciences to social...
Decisions about alpha level may be different, especially as it relates from hard sciences to social sciences. For example, a medical trial for cancer treatments conducts their statistical tests at .0001 – so for every 1 out of 10,000 patients, there may be issues, sickness or even death. For social science, we use alpha .05. We are comfortable with performing research, for example, on students. So we are satisfied with losing 5 out of 100 students or having our results...
Decisions about alpha level may be different, especially as it relates from hard sciences to social...
Decisions about alpha level may be different, especially as it relates from hard sciences to social sciences. For example, a medical trial for cancer treatments conducts their statistical tests at .0001 – so for every 1 out of 10,000 patients, there may be issues, sickness or even death. For social science, we use alpha .05. We are comfortable with performing research, for example, on students. So we are satisfied with losing 5 out of 100 students or having our results...
Decisions about alpha level may be different, especially as it relates from hard sciences to social...
Decisions about alpha level may be different, especially as it relates from hard sciences to social sciences. For example, a medical trial for cancer treatments conducts their statistical tests at .0001 – so for every 1 out of 10,000 patients, there may be issues, sickness or even death. For social science, we use alpha .05. We are comfortable with performing research, for example, on students. So we are satisfied with losing 5 out of 100 students or having our results...
Decisions about alpha level may be different, especially as it relates from hard sciences to social...
Decisions about alpha level may be different, especially as it relates from hard sciences to social sciences. For example, a medical trial for cancer treatments conducts their statistical tests at .0001 - so for every 1 out of 10,000 patients, there may be issues, sickness or even death. For social science, we use alpha .05. We are comfortable with performing research, for example, on students. So we are satisfied with losing 5 out of 100 students or having our results...
Decisions about alpha level may be different, especially as it relates from hard sciences to social...
Decisions about alpha level may be different, especially as it relates from hard sciences to social sciences. For example, a medical trial for cancer treatments conducts their statistical tests at .0001 – so for every 1 out of 10,000 patients, there may be issues, sickness or even death. For social science, we use alpha .05. We are comfortable with performing research, for example, on students. So we are satisfied with losing 5 out of 100 students or having our results...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT