Assignment 19
Due Tuesday, December 6 at 3:20pm on Catalyst

Assignment 19.1: Passwords (10pts)

  • A simple technique for creating a password that is difficult to guess is to pick a memorable phrase that is at least 4 words long, such as "I love cookies a lot."
  • Then, remove all whitespace from your phrase: "Ilovecookiesalot"
  • Finally, replace all vowels with other characters from the keyboard following these rules:
  1. Replace every 'a' or 'A' character with a '@'
  2. Replace every 'e' or 'E' character with a '3'
  3. Replace every 'o' or 'O' character with a '0'
  4. Replace every 'i' or 'I' character with a '!'
  5. Replace every 'u' or 'U' character with a '^'
  • By applying these rules, the resulting password would be "!l0v3c00kI3s@l0t"
  • On a piece of paper, pick a phrase of your own, convert it to a password using the above system, and then test its strength with the password checker found here.
  • For more tips on creating good passwords, see the discussion here.
  • For this assignment, we are going to create a program that reads in phrases in a file, converts the phrases to passwords using the above rules, and then outputs the passwords to a new file.
  • We are also going to practice the new I/O skills we just learned.
  • Open up a new C++ file in CodeBlocks and name it password.cpp.
  • Add the following starter code to your file:

/*
* Name
* Section
*/

#include <iostream>
using namespace std;

int main() {

    string phrase;

    return 0;
}

  • Next, create a new text file, named phrases.txt. Your output file should be named passwords.txt.
  • Inside the phrases.txt file, add the following three phrases, each on its own line of the file:
I love cookies a lot
You are so awesome
Writing code is fun
  • Inside of passwords.cpp add the appropriate libraries.
  • Then, between the curly braces of main, we will need to add code to open phrases.txt  for reading and passwords.txt for writing. Make sure you check for failure when opening these files.
  • Next, add a loop to read in the phrases from phrases.txt and store them one-by-one as the phrase variable.
  • Why do you want to use getline here rather than fin?
  • Inside your while loop, we want to add a for loop to iterate through our line variable character by character.
  • Hint: You will need to use a for loop, the string length function and string indexing here.
  • Now, inside this for loop, we will add a series of if-else if and else statements.

if (phrase[i] == ' ') {

    //do nothing as we wish to remove blank space

} else if(phrase[i] == 'a' || phrase[i] == 'A') { //convert to @ symbol

    cout << "@";

}

//you fill in the missing else ifs here

else { //all other letters leave alone

    cout << phrase[i];

}

  • Add in the missing else ifs and then run your program to test it.
  • Note that you will need to add a cout << endl at bottom of your while loop to put each password on a new line.
  • Finally, replace any couts with fouts.
  • Make sure you close your input and output streams at the end of the program.
  • Where should you add an additional fout statement to print "The passwords are:" at the top of the output file?
  • Submit password.cpp to Catalyst when you are finished.


When you are finished, your passwords.txt file should contain the following:



Assignment 19.2 Function Worksheet (10 pts)
  • Copy and paste the starter code into a new file called funFunctions.cpp
  • Write the required functions as described by the prototypes and comments.
  • The functions should be written below main.
  • Then, run the code when you are finished to see if you wrote the functions correctly.
  • Check the test results and make any alterations to your functions as necessary.
  • When all of the tests pass, upload your code to Catalyst.

/**
 *
 * CIS 22A
 */

#include <iostream>
using namespace std;

int sumDouble(int a, int b);
//Given two int values, return their sum.
//Unless the two values are the same, then return double their sum.
//sumDouble(1, 2) → 3
//sumDouble(3, 2) → 5
//sumDouble(2, 2) → 8

bool makes10(int a, int b);
//Given 2 ints, a and b, return true if one of them is 10
// or if their sum is 10.
//makes10(9, 10) → true
//makes10(9, 9) → false
//makes10(1, 9) → true


bool monkeyTrouble(bool aSmile, bool bSmile);
//We have two monkeys, a and b, and the parameters
//aSmile and bSmile indicate if each is smiling.
//We are in trouble if they are both smiling
//or if neither of them is smiling.
//Return true if we are in trouble.
//monkeyTrouble(true, true) → true
//monkeyTrouble(false, false) → true
//monkeyTrouble(true, false) → false


bool or35(int a);
//Return true if the given non-negative number is a multiple of 3
//or a multiple of 5. Use the % "modulus" operator
//or35(3) → true
//or35(10) → true
//or35(8) → false


string notString(string str);
//Given a string, return a new string where "not " has been added to the front.
//However, if the string already begins with "not", return the string unchanged.
//notString("candy") → "not candy"
//notString("x") → "not x"
//notString("not bad") → "not bad"


string frontBack(string str);
//Given a string, return a new string where the first and last chars have been exchanged.
//frontBack("code") → "eodc"
//frontBack("a") → "a"
//frontBack("ab") → "ba"


bool hasTeen(int num1, int num2, int num3);
//We'll say that a number is "teen" if it is in the range 13..19 inclusive.
//Given 3 int values, return true if 1 or more of them is/are teen.
//hasTeen(13, 20, 10) → true
//hasTeen(20, 19, 10) → true
//hasTeen(20, 10, 13) → true



int main()
{
    int result;
    bool answer;
    string value;
    cout << "***Testing sumDouble***"<< endl << endl;
    result = sumDouble(1, 2);
    cout << "Should print 3: " << result << endl;
    result = sumDouble(3, 2);
    cout << "Should print 5: " << result <<endl;
    result = sumDouble(2, 2);
    cout << "Should print 8: " << result << endl << endl;
   
    cout << "***Testing makes10***"<< endl << endl;
    answer = makes10(9, 10);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = makes10(9, 9);
    cout << boolalpha << "Should be false: " << answer << endl;
    answer = makes10(1, 9);
    cout << boolalpha << "Should be true: " << answer << endl;
       
    cout << "***Testing monkeyTrouble***"<< endl << endl;
    answer = monkeyTrouble(true, true);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = monkeyTrouble(false, false);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = monkeyTrouble(true, false);
    cout << boolalpha << "Should be false: " << answer << endl << endl;
   
    cout << "***Testing or35***"<< endl << endl;
    answer = or35(3);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = or35(10);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = or35(8);
    cout << boolalpha << "Should be false: " << answer << endl << endl;
   
    cout << "***notString***"<< endl << endl;
    value = notString("candy");
    cout << "Should be not candy: " << value << endl;
    value = notString("x");
    cout << "Should be not x: " << value << endl;
    value = notString("not bad");
    cout << "Should be not bac: " << value << endl << endl;
   
    cout << "***frontBack***"<< endl << endl;
    value = frontBack("code");
    cout << "Should be eodc: " << value << endl;
    value = frontBack("a");
    cout << "Should be a: " << value << endl;
    value = frontBack("ab");
    cout << "Should be ba: " << value << endl << endl;
   
   
    cout << "***Testing or35***"<< endl << endl;
    answer = hasTeen(13, 20, 10);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = hasTeen(20, 19, 10);
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = hasTeen(20, 10, 13) ;
    cout << boolalpha << "Should be true: " << answer << endl;
    answer = hasTeen(20, 10, 45) ;
    cout << boolalpha << "Should be false: " << answer << endl << endl;
   
    cout << "***End of Tests***" << endl;

    return 0;

}