Assignment 18
Due Monday, March 19 at 9:20am on Canvas


Assignment 18.1: Functions Worksheet 3 (10pts)

  • Copy and paste the starter code into a new file called funFunctions3.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, submit your program.
/**
 *
 * CIS 22A
 */

#include <iostream>
using namespace std;

bool isLetter(char letter);
//Given a char, uses ASCII to determine if it is a letter
//Returns true if is a letter (a-z or A-Z)
//Returns false if it is not a letter
//isLetter('d') -> true
//isLetter('D') -> true
//isLetter('!') -> false

bool isNumber(char num);
//Given a char, uses ASCII to determine if it is a number
//Returns true if is a number (0-9)
//Returns false if it is not a number
//isNumber('a') -> false
//isNumber('3') -> true

void capitalizeLetter(char& letter);
//Given a char that is a valid letter (a-z or A-Z)
//Capitalizes the letter if it is a lower case,
//Leaves the letter unaltered if it is an upperCase
//capitalizeLetter('a') - > 'A'
//capitalizeLetter('A') -> 'A'

void capitalizeString(string& str);
//Given a string, capitalizes all letters in the string
//Hint: Look back at your Assignment 14
//capitalizeString("I love cookies!") - > "I LOVE COOKIES!"
//capitalizeString("124 East Central Ave") -> "124 EAST CENTRAL AVE"

void spaceToComma(string& list);
//Given a string, alters any blank spaces in the string
//to be commas. Hint: Use a for loop, and string indexing
//spaceToComma("cats hats bats") -> "cats,hats,bats"
//spaceToComma("I love cookies! ") -> "I,love,cookies!,"

void plusOut(string& str);
//Given a string, converts any number in the string to a '+'
//Hint: Use a for loop, and string indexing
//plusOut("123 East 9th St")-> "+++ East +th St"
//
plusOut("Agent 007")-> "Agent +++"

int main()
{
    int result;
    bool answer;
    string value;
  
    cout << boolalpha << "***Testing isLetter***"<< endl << endl;
    answer = isLetter('d');
    cout << "Should be true: " << answer << endl;
    answer = isLetter('D');
    cout << "Should be true: " << answer << endl;
    answer = isLetter('!');
    cout << "Should be false: " << false << endl;

    cout << "***Testing isNumber***"<< endl << endl;
    answer = isNumber('a');
    cout << "Should be false: " << answer << endl;
    answer = isNumber('9');
    cout << "Should be true: " << answer << endl << endl;

    cout << "***Testing capitalizeLetter***"<< endl << endl;
    char lower = 'a';
    capitalizeLetter(lower);
    cout << "Should print A: " << lower << endl;
    char upper = 'A';
    capitalizeLetter(upper);
    cout << "Should print A: " << upper << endl << endl;
   
    cout << "***Testing capitalizeString***"<< endl << endl;
    string str = "I love cookies!";
    capitalizeString(str);
    cout << "Should print I LOVE COOKIES!: " << str << endl;
    str = "124 East Central Ave"
    capitalizeString(str);
    cout << "Should print 124 EAST CENTRAL AVE: " << str << endl << endl;

    cout << "***Testing spaceToComma***"<< endl << endl;
    str = "cats hats bats";
    spaceToComma(str);
    cout << "Should be cats,hats,bats: " << str << endl;
    str = "I love cookies! ";
    spaceToComma(str);
    cout << "Should be I,love,cookies!,: " << str << endl << endl;


    cout << "***Testing plusOut***"<< endl << endl;
    str = "
123 East 9th St";
    plusOut(str);
    cout << "
+++ East +th St: " << str << endl;
    str = "
Agent 007";
    plusOut(str);
    cout << "Should be
Agent +++: " << str << endl << endl;
   
    cout << "***End of Tests***" << endl;
  
    return 0;

}

Assignment 18.2: Restaurant Math (10 pts)
  • Write a program to help you calculate your bill at a restaurant.
  • Name your program restaurant.cpp.
  • For this program, you will need to write 4 functions, as follows:

calculateBill
  • Takes in a double reference parameter for the price of the food.
  • Takes in a double value parameter for the chosen tip percent
  • calls the addTax function to add taxes to the price.
  • calls the calculateTip function to add the tip to the price.
  • calls the formatPrice function to print out the price
  • returns nothing

addTax

  • Takes in a double reference parameter for the price of the food.
  • Updates the price by adding on the tax, assuming the tax rate is 9.25% in Santa Clara County
  • returns nothing

calculateTip

  • Takes in a double reference parameter for the current bill (price + tax).
  • Takes in a second double - this time a value parameter - for the percent tip.
  • Calculates the amount of the tip and updates the current price by adding on the tip amount
  • returns nothing

formatPrice

  • Takes in a double value parameter for the price.
  • prints the price in the form $X.XX
  • (Hint: How do you get the price to have only 2 decimal values?)
  • Also prints a message about the final price: "With tax and tip, your total comes to... $<price>"
  • returns nothing

Additional Specifications

  • You must include function prototypes at the top of your program.
  • Each prototype must also have a comment in the style described in class.
  • You should write the full function definitions below main.
  • Important: You should call the calculateBill function inside of main, not the other three functions.
  • The other 3 functions should be called inside of the calculateBill function.

Starter Code:

  • Below is the starter code for this assignment.
  • Important: Do NOT change the code in any way. You should simply add your three additional functions to it.
  • Also important: Do NOT add anything to main and do not alter main.
  • Your program should work identically to mine without making any changes to main.
  • Note that calculateTip will work similarly to the addTax function, except you will have to use the chosen tip amount in your calculations.


void addTax(double& price);
//calculates the tax at 9.25% and adds it to the price of the meal

void calculateBill(double& price, double percent);
//calls addTax, calculateTip and displayPrice
//to calculate and display the final price


int main()
{
    cout << "Welcome to the Restaurant Calculator\n";
    cout << "\nEnter the price of your meal: ";
    double price;
    cin >> price;
    double percent;
    cout << "Enter the percent tip (10-25): ";
    cin >> percent;
    calculateBill(price, percent);
    cout << "Goodbye!";

}

void addTax(double& price)
{
    price *= 1.0925;

}


void calculateBill(double& price, double percent)
{
    addTax(price);
    //add two additional function calls here

}

  • Your program should  now work identically to the following:
Welcome to the Restaurant Calculator!

Enter the price of your meal: 25.99

Enter the percent tip (10-25): 20


With tax and tip, your total comes to... $34.07

Goodbye!

  • However, there is a potential problem we have not accounted for in our program. What if the user enters a string instead of a double for the meal cost and the percent tip.
  • Add in two while loops into your program to check for cin failure.
  • Print an appropriate error message to the user as shown below.
  • When you are finished your program should work identically to the following:

Sample Output

  • When your program works identically to the output below, submit it to Canvas.
Welcome to the Restaurant Calculator

Enter the price of your meal: twenty-five ninety-nine
Please enter numbers not letters.

Enter the price of your meal: twenty-five dollars and ninety-nine cents!
Please enter numbers not letters.

Enter the price of your meal: $25.99
Please enter numbers not letters.

Enter the price of your meal: 25.99

Enter the percent tip (10-25): twenty
Please enter numbers not letters.

Enter the percent tip (10-25): twenty percent!
Please enter numbers not letters.

Enter the percent tip (10-25): I said twenty percent!!!
Please enter numbers not letters.

Enter the percent tip (10-25): 20

With tax and tip, your total comes to... $34.07
Goodbye!