Question

In: Computer Science

Modify the provided code to create a program that calculates the amount of change given to...

Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount.

Use three functions:

• bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false.

• float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal, and returns the grand total with sales tax (8.25%).

• float calcChange(float, float) – Takes total and the amount paid and returns the change. Amount paid must be higher than the total.

IMPORTANT!

• The functions should not print any value. All the values are printed from the main function.

• Do not modify the main function. Write the functions such that they work in the code provided

Sample Output:

Welcome to the Vend-o-matic! Here are your choices:

a) Ice Cream - $2.75

b) Candy - $1.25

c) Soda - $1.50

d) Gum - $0.50

Please select an item:

b Enter Quantity: -1

ERROR: Invalid Quantity. Please reenter: 2

Total with tax: $2.71

Enter Payment Amount: $2.00

ERROR: Insufficient funds. Please reenter payment: $3.00

Your change due is: $.29

_____________________________________________________________________

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

// Function Prototypes:
bool isValidChoice(char);
float calcTotal(int, float);
float calcChange(float, float);

int main()
{
char choice;
int quantity;
float total, payment_amount, change, item_cost;

cout << "Welcome to the Vend-o-matic! Here are your choices:" << endl
<< "a)Ice Cream - $2.75" << endl
<< "b)Candy - $1.25" << endl
<< "c)Soda - $1.50" << endl
<< "d)Gum - $0.50" << endl
<< "Please select an item: ";
cin >> choice;
while(!isValidChoice(choice)) // Function Call
{
cout << "Error: Invalid choice. Please select a valid item: ";
cin >> choice;
}

if(choice == 'a')
{
item_cost = 2.75;
}
else if(choice == 'b')
{
item_cost = 1.25;
}
else if(choice == 'c')
{
item_cost = 1.50;
}
else if(choice == 'd')
{
item_cost = 0.50;
}

cout << endl << "Enter Quantity: ";
cin >> quantity;

while(quantity < 1 || quantity > 20)
{
cout << "ERROR: Invalid Quantity. Please re-enter: ";
cin >> quantity;
}

total = calcTotal(quantity, item_cost); // Function Call

cout << fixed << setprecision(2) << "Total with tax: $" << total << endl;

cout << "Enter Payment Amount: $";
cin >> payment_amount;

while(payment_amount < total)
{
cout << "ERROR: Insufficient funds. Please re-enter payment: $";
cin >> payment_amount;
}

change = calcChange(total, payment_amount); // Function Call

cout << "Your change is: $" << change;

return 0;
}

// TODO: add function definitions here

Solutions

Expert Solution

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

// Function Prototypes:
bool isValidChoice(char);
float calcTotal(int, float);
float calcChange(float, float);

int main()
{
    char choice;
    int quantity;
    float total, payment_amount, change, item_cost;

    cout << "Welcome to the Vend-o-matic! Here are your choices:" << endl
    << "a)Ice Cream - $2.75" << endl
    << "b)Candy - $1.25" << endl
    << "c)Soda - $1.50" << endl
    << "d)Gum - $0.50" << endl
    << "Please select an item: ";
    cin >> choice;
   
    while(!isValidChoice(choice)) // Function Call
    {
    cout << "Error: Invalid choice. Please select a valid item: ";
    cin >> choice;
    }

    if(choice == 'a')
    {
        item_cost = 2.75;
    }
    else if(choice == 'b')
    {
        item_cost = 1.25;
    }
    else if(choice == 'c')
    {
        item_cost = 1.50;
    }
    else if(choice == 'd')
    {
        item_cost = 0.50;
    }

    cout << "Enter Quantity: ";
    cin >> quantity;

    while(quantity < 1 || quantity > 20)
    {
        cout << "ERROR: Invalid Quantity. Please re-enter: ";
        cin >> quantity;
    }

    total = calcTotal(quantity, item_cost); // Function Call

    cout << fixed << setprecision(2) << "Total with tax: $" << total << endl;

    cout << "Enter Payment Amount: $";
    cin >> payment_amount;

    while(payment_amount < total)
    {
    cout << "ERROR: Insufficient funds. Please re-enter payment: $";
    cin >> payment_amount;
    }

    change = calcChange(total, payment_amount); // Function Call

    cout << "Your change due is: $" << change;

    return 0;
}

bool isValidChoice(char ch){
    
    //checks if ch is equal to a,b,c,d then rreturns true
    //otherwise returns false
    if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd'){
        
        return true;
    }
    else{
        return false;
    }
}

float calcTotal(int qty, float cost){
    
    //multiply qty with cost and store it in temp variable
    float temp = qty * cost;
    
    //calculates 8.25% sales tac
    float tax =  (qty * cost *8.25)/100;
    
    //calculates final amout with tax and store it in ans variable
    float ans = temp + tax;
    
    //return ans
    return ans;
    
}

float calcChange(float total, float payable_amt){
    
    //calculates change amount
    float change = payable_amt - total;
    
    //return change
    return change;
}

OUTPUT:


Related Solutions

modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
In C Program #include Create a C program that calculates the gross and net pay given...
In C Program #include Create a C program that calculates the gross and net pay given a user-specified number of hours worked, at minimum wage. The program should compile without any errors or warnings (e.g., syntax errors) The program should not contain logical errors such as subtracting values when you meant to add (e.g., logical errors) The program should not crash when running (e.g., runtime errors) When you run the program, the output should look like this: Hours per Week:...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. use OPTIONAL TYPE and NULL object design pattern.   import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map aTags = new HashMap<>(); public Student(String pName) { aName =...
Java: modify the given example code to make it possible to create a student object by...
Java: modify the given example code to make it possible to create a student object by only specifying the name, all other info may be absent. it may also be possible to add a tag with an absent value. import java.util.HashMap; import java.util.Map; public class Student { private final String aName; private String aGender; private int aAge; private Country aCountry; private Map<String, String> aTags = new HashMap<>(); public Song(String pName) { aName = pName; } public String getName() { return...
Program in Java code Write a program with total change amount in pennies as an integer...
Program in Java code Write a program with total change amount in pennies as an integer input, and output the change using the fewest coins, one coin type per line. The coin types are Dollars, Quarters, Dimes, Nickels, and Pennies. Use singular and plural coin names as appropriate, like 1 Penny vs. 2 Pennies. .Ex1: If the input is: 0 the output is:    No change            Ex2: If the input is:    45   the output is:   1 Quarter 2 Dimes
Create a small program that calculates the final grade in this course, given the different assignments...
Create a small program that calculates the final grade in this course, given the different assignments and exams you will be required to take. The program should ask for all the grades through the semester, and as a final output it should compute the weighted average as well as give the final letter grade. You should use the syllabus for this course to calculate the final grade. • Five assignments worth 30 points each. • Three projects worth 100 points...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
Modify the object provided in the code below to include a variety of overloaded operators in...
Modify the object provided in the code below to include a variety of overloaded operators in C++. Your object should have the following member variables: 1. float *arr a. A dynamic float array that constitutes the data in your myArray object. 2. int size a. The size of the array that the myArray object holds Modify the provided code to include a variety of overloaded operators Operators to overload: 1. bool operator!=(myArray& obj2) a. Tests to see if the calling...
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Create an application that calculates and displays the amount of a homeowner’s property tax. The tax...
Create an application that calculates and displays the amount of a homeowner’s property tax. The tax is 1.35% of the property’s assessed value, which will be entered by the user. a. Prepare a Planning Chart for the application. b. Draw a sketch of an appropriate interface. Be sure to follow the GUI design guidelines covered in the chapter. The guidelines are summarized in Figure 2-20. (If you want to include an image in the interface, you can either use your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT