Assignment 18
Due Thursday, November 30 at 3:20pm on Canvas


Assignment 18.1: Functions Worksheet 2 (10pts)

  • Copy and paste the starter code into a new file called funFunctions2.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.4
//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!,"

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 << "***End of Tests***" << endl;
  
    return 0;

}

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

calculate_bill
  • Takes in a double reference parameter for the price of the food.
  • calls the add_tax function to add taxes to the price.
  • calls the calculate_tip function to add the tip to the price.
  • calls the format_price function to print out the price
  • returns nothing

add_tax

  • Takes in a double reference parameter for the price of the food.
  • adds the tax onto the the total price, assuming the tax rate is 8.5%
  • returns nothing

calculate_tip

  • Takes in a double reference parameter for the current bill (price + tax).
  • adds the tip onto the the total price, assuming a tip of 15%
  • returns nothing

format_price

  • 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 calculate_bill function inside of main, not the other three functions.
  • The other 3 functions should be called inside of the calculate_bill 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 calculate_tip will work very similarly to the add_tax function.



void add_tax(double& pizza_price);
//calculates the tax at 8.5% and adds it to the price of the pizza


void calculate_bill(double& pizza_price);
//calls add_tax, calculate_tip and display_price
//to calculate and display the final price


int main()
{
    cout << "Welcome to Mama Domino's Pizzaria!\n";
    cout << "\nEnter the price of the pizza: ";
    double price;
    cin >> price;
    calculate_bill(price);
   
}

void add_tax(double& pizza_price)
{
    pizza_price *= 1.085;
  
}


void calculate_bill(double& pizza_price)
{
    add_tax(pizza_price);

    //add two additional function calls here

}

Sample Output

  • Your program should work identically to the following:

Welcome to Mama Domino's Pizzaria!

Enter the price of the pizza: 12.99

With taxes and tip, your total comes to $16.21

  • When you are finished, submit your program.