Assignment 20
Due Tuesday, June 19th at 9:20am on Canvas
Assignment 20.1: Functions Worksheet 4 (10pts)
- Copy and paste the starter code into a new file called funFunctions4.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;
void deFront(string& str); //Given a string, remove its first two letters //deFront("Hello") → "llo" //deFront("java") → "va" //deFront("away") → "ay"
void withoutX(string& str); //Given a string, if the first or last chars are 'x' or 'X', //remove those 'x'/'X' chars. Otherwise, leave the string unchanged //withoutX("xHix") → "Hi" //withoutX("xHi") → "Hi" //withoutX("Hxix") → "Hxi"
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 +++"
void remove1At(string& str); //Given a string, replace an i or I character with a 1 //and any a or A character with the @ sign. //Hint: use a for loop, and string indexing //remove1At("Hi, Brad") -> "H1, Br@d" //remove1At("I love fun AND games") -> "1 love fun @ND g@mes"
int main() { int result; bool answer; string value, str;
cout << boolalpha << "***Testing deFront***" << endl << endl; value = "Hello"; deFront(value); cout << "Should print llo: " << value << endl; value = "java"; deFront(value); cout << "Should print va: " << value << endl; value = "away"; deFront(value); cout << "Should print ay: " << value << endl;
cout << boolalpha << "***Testing withoutX***" << endl << endl; value = "xHix"; withoutX(value); cout << "Should print Hi: " << value << endl; value = "xHi"; withoutX(value); cout << "Should print Hi: " << value << endl; value = "Hxix"; withoutX(value); cout << "Should print Hxi: " << value << 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 << "***Testing remove1At***" << endl << endl; str = "Hi, Brad"; remove1At(str); cout << "Should print H1, Br@d: " << str << endl; str = "I love fun AND games"; remove1At(str); cout << "Should print 1 love fun @ND g@mes: " << str << endl << endl;
cout << "***End of Tests***" << endl;
return 0;
} Assignment 20.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!
Assignment 20.3: Barnyard Animals (10 pts)5 3 8 2 4
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 output file outfile.txt
- 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 - Submit your program to Canvas when you are finished
|