Assignment 10

Due Tuesday, November 1 at 3:20pm on Catalyst



Assignment 10.1: Making a Receipt (10 pts)

  • Let's write a program to create a receipt.
  • Find your cookies.cpp assignment by downloading it from Catalyst. We are going to alter it to include prices and a total.
  • Scroll down to the bottom to view the outcome of this program to give you a sense of what you need to accomplish.
  • Start by declaring 4 new double variables at the top of your program.
  • These variables will be used to store the total price of the user's order of each cookie. For example:
    • Note that you will need to declare two more double variables for the other two cookie flavors.
    • We are also going to create some new constant double variables to store the price of the cookies.
    • Create four more variables like this one - one for the price of each cookie:

        const double PRICE_CHOC_CHIP = 1.95;

    • Why are we using a const here? Will these prices ever change in the program?
    • Also, note that these variables need to be capitalized to use good programming style.
    • Finally, declare a double called total to store the total price for the entire customer purchase.
    • Below your variable declarations, you should have two cout statements to welcome your user to the program. Your statements should produce output identical to the following:
    • Next, we need to add information about prices. Add the following cout statement below the other two. 
    • Note the use of \n in the above statement. What does each one do?
    • Now, write four cout statements to declare the price of each cookie. Each statement should look like the statement below:

    • When you are finished, you should have five cout statements, which print out the following. Make sure your output is identical!
    • Next, you need to collect the user's order. 
    • If you don't already have it, add a cout statement to prompt the user to enter how many chocolate chip cookies they would like, followed by a cin statement to store the information given by the user as your chocolate chip variable. Your code should look similar to the following (your variable name may be different).
    • If you don't already have them in your program, add three more pairs of cout-cin statements for the other three flavors of cookie.
    • Your program's output should now look identical to the following (except for user input). Please alter your program or ask for help if your program's output does not look like the image below:
    • Next, it is time to calculate the amount the user will need to pay for each cookie. To find the subtotal for each cookie, we need to multiply the price of the cookie by the number of cookies the user wishes to order. Below your last cin statement, add a statement like the following to your program (Note - your variable names may be different!).
    choc_total = num_choc_chip * PRICE_CHOC_CHIP;
    • Add three more statements, similar to the one above, one for each of the remaining cookie types.

    The rest of this program will be dedicated to printing out the receipt.
    • Add the following cout statement:
    • What do the two \n do in the above statement?
    • Because we are printing out decimal values, we will need to use fixed and setprecision(n).
    • Add a new library to the top of your program:
    #include <iomanip>
    • Add a cout statement to set the precision under which you want the output to display.

    cout << fixed << setprecision(???); //What number should replace the ???

    • Add a second cout statement to display the type of cookie, followed by a colon, followed by the number ordered, followed by the @ symbol, followed by the price of the cookie, followed by the word "each", followed by the subtotal for that cookie flavor. To make everything line up nicely, we will make heavy use of the tab \t.
    • Your cout statement should look like the following:
    • If you don't understand this cout statement, please ask for the instructor's help.
    • Add three similar cout statements for the remaining cookie flavors.
    • The output of your program should now be identical to the following:


    Welcome to Cookie Magic, where every bite of cookie is magical!
    Allow me to assist you with your order!

    We sell the following flavors:
    Chocolate Chip Cookies -- $1.95 each
    Chocolate Mint Cookies -- $2.15 each
    Peanut Butter Cookies -- $2.05 each
    Oatmeal Raisin Cookies -- $1.85 each

    Please enter the number of chocolate chip cookies you would like to purchase: 3
    Please enter the number of chocolate mint cookies you would like to purchase: 2
    Please enter the number of peanut butter cookies you would like to purchase: 5
    Please enter the number of oatmeal raisin cookies you would like to purchase: 1

    You Ordered the Following:
    Chocolate Chip Cookies: 3 @ $1.95 each... $5.85
    Chocolate Mint Cookies: 2 @ $2.15 each... $4.30
    Peanut Butter Cookies: 5 @ $2.05 each... $10.25
    Oatmeal Raisin Cookies: 1 @ $1.85 each... $1.85

    • We also need to calculate the total price to display to the user. Add the subtotal for each cookie together and assign the result to the total variable. How would you do this? Complete the statement below:
    total = choc_total + mint_total; \\You fill in the rest
    • Now, we need to print out the total price. Below your last cout statement, add the following one:

    • Have we added enough \t to make the total line up with the subtotal for each cookie? The answer is no. Add more \t to the above cout statement until the total lines up with the subtotals.
    • Finally, let's thank the user for purchasing from us. Add a final cout statement to your program.
    • The output of your program should look identical to the one below. If it is not identical, then you will need to fix it!
    • When you are finished, submit to Catalyst


    Welcome to Cookie Magic, where every bite of cookie is magical!
    Allow me to assist you with your order!

    We sell the following flavors:
    Chocolate Chip Cookies -- $1.95 each
    Chocolate Mint Cookies -- $2.15 each
    Peanut Butter Cookies -- $2.05 each
    Oatmeal Raisin Cookies -- $1.85 each

    Please enter the number of chocolate chip cookies you would like to purchase: 3
    Please enter the number of chocolate mint cookies you would like to purchase: 2
    Please enter the number of peanut butter cookies you would like to purchase: 5
    Please enter the number of oatmeal raisin cookies you would like to purchase: 1

    You Ordered the Following:
    Chocolate Chip Cookies: 3 @ $1.95 each...    $5.85
    Chocolate Mint Cookies: 2 @ $2.15 each...    $4.30
    Peanut Butter Cookies: 5 @ $2.05 each...     $10.25
    Oatmeal Raisin Cookies: 1 @ $1.85 each...    $1.85

    Total:                                       $22.25

    Thank you for your order! Please come again!

Assignment 10.2: Weight in Space (10 pts)
  • Remember our class activity where we calculated a user's weight on the moon?
  • Let's see if we can expand it to provide more options for the user.
  • How does our weight change on different planets?
  • Our assignment will make use of many of the skills we have already learned:
    • multiway branches (if-else if-else clauses)
    • strings and string comparison
    • arithmetic operators and expressions
    • logical operators
  • Begin your program by declaring a double variable named weight.
  • Also, declare a string variable named planet.
  • Next, welcome the user and explain that this program will calculate his/her weight on different planets given his/her weight on Earth. (See sample output below)
  • Now, let's prompt the user to input a weight in pounds and store the input as the weight variable. (See sample output below)
  • Next, provide the user with a choice of the various planets on which the program can calculate his/her weight. The options are MercuryVenusMarsJupiterSaturnUranusNeptune, and Pluto.
  • You will need to tell the user specifically what their options are by listing out the planets. Note that I used the \t to get the indentation of each of the planet names in my example output below. (See sample output below)
  • Now, prompt the user to input his/her choice of planet. (See sample output below)
  • Store the user choice as the planet variable.
  • Next, you will need a series of if-else if-else statements, like the following:
if (planet == "Mercury") {
    weight = weight * 0.38;
} else if (planet == "Venus") {
    //statement goes here
}
//rest of else ifs go here
} else {
    //statement goes here    
}
  • Each if tests the user input to determine whether it is equivalent to one of the planets. For example:
if (planet == "Mercury")
  • Since the user input is a string, you need to do string comparison (string == string).
  • You should also allow your users to input a lower case value (planet == "mercury").
  • How can I test for the value "Mercury" or "mercury" in a single test condition of an if statement? (Hint: What logical operator do I need here: &&, || or ! ?)
  • Inside each if clauseyou will need to make the calculation for the weight on that particular planet.
  • To do so, you will need to know the conversion rate for each of the planets. The conversion is as follows:
    • Mercury - multiply by 0.38
    • Venus - multiply by 0.91
    • Mars - multiply by 0.38
    • Jupiter - multiply by 2.54
    • Saturn - multiply by 1.08
    • Uranus - multiply by 0.91
    • Neptune - multiply by 1.19
    • Pluto - multiply by 0.06
  • Note that to avoid magic numbers, you will need to declare a const variable for each of the multipliers aboveYour const variables should look like this:
const double MERCURY_MULT = 0.38;
  • You will need to use those multipliers inside your if and else if statements to calculate the user weight for each planet.
  • Finally, your else clause gives you the opportunity to do some error checking on the user input. Essentially, to arrive at the final else condition, the user must have input a string that was not equal to any of the planets.
  • Inside your else clause, output the error message: "You entered an invalid name for a planet. Please re-run the program to try again." (See sample output)
  • Don't forget to add a return 1; statement inside your else clause to terminate the program if the user provides an invalid planet name.
  • Finally, at the bottom of your program, after and outside of all of your if-else if-else clauses, you need to print out the user's weight on the planet of his/her choice.
  • To do so, above your return 0; statement, add a cout statement to print out "You weigh X.X pounds on <Insert Planet Name Here>." (See sample output below)
  • Important: The output for the weight should only have one number after the decimal point. Therefore, you need to make use of the fixed and setprecision() manipulators
  • Don't forget to include the iomanip library at the top of your program.
  • Finally, take a moment to make sure your indentation is correct. Your indentation should look like the following

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
     if (test condition) {
         //statements

     } else if {
         //statements

     } else if {
         //statements

     } else {
         //statements

     }
}
  • Run your program. Your output should look identical to the following:

  • Alternately, your program should output the following if the user provides an invalid entry:



Assignment 10.3: Rock Paper Scissors (10 pts)

  • Write a program to simulate a Rock, Paper, Scissors competition between two players.
  • First, the program will welcome them to the competition.
Let's play Rock-Paper-Scissors!
  • Then, the program will prompt each user to enter his or her name.
Player one, please enter your name: _
  • Next, the program will ask each user (referring to him or her by name) to enter "Rock," "Paper" or "Scissors."
Jorge, please enter "Rock", "Paper" or "Scissors": _
  • Important: Note the use of the player name above and the " marks.
  • The program will determine who the winner is and output one of three options:
Rock breaks scissors.
Paper covers rock.
Scissors cut paper.
  • How will the program determine the winner? Hint: if - else if - else statements and using logical operators (&&, ||, !).
  • After outputting the above statement, the program will announce the winner by name:
Jorge wins!
  • In the case of a tie, the program should announce that the result is a draw.
The game is a tie.
  • Important: You are required to use logical operators (&&, ||, !) as part of your if  and else if statements.
  • When you are finished, your program output should look identical to the output below:
  • Alternately,