Assignment 18
Due Thursday, December 1 at 3:20pm on Catalyst

Assignment 18.1: 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.
  • 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.
  • format

/**

    calculates the tax at 8.5% and adds it to the price of the pizza


    @param pizza_price the price of the pizza
*/
void add_tax(double& pizza_price);

/**
    calls add_tax, calculate_tip and display_price
    to calculate and display the final price

    @param pizza_price the price of the pizza as entered by the user
 
*/
void calculate_bill(double& pizza_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, upload your program to Catalyst.


Assignment 18.2: Barnyard Animals (10 pts)

  • In this assignment we write a program that copies five numbers from an input stream to an output stream.
  • Copy the following program into a C++ file, save it as animals.cpp, and then compile and run the starter program to make sure you copied it correctly.
    #include <iostream>
    using namespace std;
    
    int main() {
        // Enter your code here
    
        return 0;
    }
    
  • Then, let's create a text file to test our program. Open up Notepad or another text editor (Word is not a good choice). Name the file infile.txt. In the file, add a list of numbers each on their own line, like this:
5
3
8
2
4
  • Important: You need to save this file in the same directory that you are executing your C++ files.
  • We will read from this file after writing our program.

  • Add the following new libraries that you need to your source code file:
    #include <fstream>   // for file I/O
    #include <cstdlib>   // for exit()
    
  • Inside main(), declare an input stream variable named fin and an output stream variable named fout.
  • Add code to connect each stream to a file using open() and check for failure:
  • Name your input and output files: infile.txt and outfile.txt respectively
  • Add statements to read two numbers from the input stream.
  • For example, here is possible code for reading the first number:
    int first;
    fin >> first;
    
  • Add 4 more pairs of statements like the ones above to read in the next 4 numbers.
  • Then, add an fout statement to print out a title at the top of your file
    fout << "Animals at our farm" << endl;
    
  • Next, write out the contents of your five variables to the file. For example:
fout << first << " chickens" << endl;
  • Add 4 more statements like the one above, so that your file contains the following when you are finished:
Animals at our farm
5 chickens
3 cows
8 ducks
2 horses
4 pigs
  • Close the streams when finished reading and writing:
    fin.close();
    fout.close();
    
  • Compile and run your program to make sure you made the changes correctly.
  • Notice that you do not see any output on the console screen. The output stream wrote the program output to the output file.
  • Using a text editor like Notepad open the output file you created and verify that your file contains the following:

    Animals at our farm
    5 chickens
    3 cows
    8 ducks
    2 horses
    4 pigs
  • Submit  your program to Catalyst when you are finished