Question: I am having trouble creating this code using functions. I can do it with if statements but not using functions. The functions must be declared

I am having trouble creating this code using functions. I can do it with if statements but not using functions. The functions must be declared before main but defined after main. Cannot use global variables. variables must be declared under main. I cannot get it to run.

Write a C++ program to work with a vending machine that accepts only a one-dollar bill and returns changes in quarters, dimes, and nickels. Upon startup of this program, the machine is filled with several coins once for each denomination from keyboard input before any purchase (quarters, dimes, and nickels). This vending machine will only accept one-dollar bill and all items cost one dollar or less, but the cost for each item must be a multiple of 5 cents. Your program would read an integer value between 0 and 100 (inclusive), representing the amount of a purchase in cents from a vending machine. Produce an error message if the input value is not in that range or it is not a multiple of 5 cents. If the input is valid, determine the amount of change that would be returned from one dollar, and print the number of quarters, dimes, and nickels. It is important to maximize the coins with the highest value first and utilize the next denomination if the current denomination is exhausted. This strategy will work for most cases, but there are a few cases where it wouldnt work (such as Q = 2, D = 4, N = 0, and you need to give back 55 cents) and you do not have to handle such situations (go ahead and reject such transaction as insufficient coins). Reject a valid purchase if it cannot be processed (not enough available coins to make the change) but program must allow more transactions. The machine must be able to keep track of an accurate count of coins in the machine at all time. Your program must allow purchases to be repeated until a sentinel value of 0 is entered. Print number of valid transactions, dollar bills, the number of coins for each denominations, and current balance just before terminating the program. Follow the format below and you must plan and write down the pseudocode before attempting your code on the computer. In addition to the main function, you must use two more reasonable functions for this project and you should try to utilize more functions, so your program is more readable. You must follow the user-interface as shown below and run those test cases at the minimum: Vending Machine Version 1. Fill up machine with changes. Please wait ... Enter number of quarters, dimes, and nickels --> 2 4 4

There are 2 quarters, 4 dimes, and 4 nickels.

Initial machine balance is $1.10.

Only one-dollar bill will be accepted.

Only amount between 0 to 100 and multiple of 5 is accepted.

Enter 0 to stop the program.

Machine is now ready. Enter a purchase amount [0 - 100] --> 35

You entered a purchase amount of 35 cents.

Please insert one-dollar bill. Processing your purchase ...

Your change of 65 cents is given as: quarter(s): 2 dime(s): 1 nickel(s): 1

Enter a purchase amount [0 - 100] --> 31

You entered a purchase amount of 31 cents.

Invalid amount (not a multiple 5). Try again. Enter a purchase amount [0 - 100] --> 35

You entered a purchase amount of 35 cents.

Please insert one-dollar bill. Processing your purchase ... Insufficient changes!

Your transaction cannot be processed.

Please take back your dollar bill. Enter a purchase amount [0 - 100] --> 75

You entered a purchase amount of 75 cents. Please insert one-dollar bill.

Processing your purchase ... Your change of 25 cents is given as: quarter(s): 0 dime(s): 2 nickel(s): 1 Enter a purchase amount [0 - 100] --> 105

You entered a purchase amount of 105 cents. Invalid amount (outside valid range). Try again. Enter a purchase amount [0 - 100] --> 0 Final report is being generated ... There are 2 valid transactions.

Number of dollars : 2 Number of quarters: 0 Number of dimes : 1 Number of nickels : 2

Machine balance : $2.20

Machine is shutting down. Good bye.

#include "pch.h" #include #include using namespace std; void startMachine(); void vendingMachine(int amount, int& cnt, int& quarters, int& dimes, int& nickels); void finalReport(int cnt, int& quarters, int& dimes, int& nickels); int main() { int quarters, dimes, nickels; int amount, cnt = 0; //Cnt is for storing no of valid transactions startMachine(); //starting the vending machine while (1) { cout << "Enter a purchase amount [0 - 100] --> "; cin >> amount; //User entering the purchase amount if (amount == 0) //If amount=0, then we print final report and break the loop { finalReport(cnt, quarters, dimes, nickels); break; } else if (amount > 100) //if amount>100, then we print error message { cout << "You entered a purchase amount of " << amount << " cents. "; cout << "Invalid amount (outside range). Try again. "; } else if (amount % 5 != 0) //if amount is not a multiple of 5, then we print error message { cout << "You entered a purchase amount of " << amount << " cents. "; cout << "Invalid amount (not a multiple of 5). Try again. "; } else //otherwise display the change { vendingMachine(amount, cnt, quarters, dimes, nickels); } } void startMachine(); //Function for taking input and filling machine with change { cout << "Vending Machine Version 1. Fill up machine with changes. "; cout << " Please wait ... Enter number of quarters, dimes and nickels --> "; cin >> quarters >> dimes >> nickels; //Taking input from user float total; total = 0.25*quarters + 0.10*dimes + 0.05*nickels; //calculating total amount in machine cout << "There are " << quarters << " quarters, " << dimes; //displaying the number of coins in machine cout << " dimes, and " << nickels << " nickels. "; cout << "Initial machine balance is $" << total << ". "; //displaying the amount cout << "Only one-dollar bill will be accepted. "; //displaying data according to the requirement cout << "Only amount between 0 to 100 and multiple of 5 is accepted. "; cout << "Enter 0 to stop the program. "; cout << "Machine is now ready. "; } void vendingMachine(int amount, int& cnt, int& quarters, int& dimes, int& nickels) //function for calculating change for a purchase amount { cout << "You entered a purchase amount of " << amount << " cents. "; //displaying the purchase amount cout << "Please insert one-dollar bill. Processing your purchase ... "; amount = 100 - amount; //as we have to calculate change for remaining amount int coin25; int coin10; int coin5; //variables for storing changes coin25 = amount / 25; //calculating number of quarters required for change if (quarters < coin25) //if less quarters are available, then we take all of them coin25 = quarters; amount = amount - 25 * coin25; //reducing amount by the 25*coin25 coin10 = amount / 10; //calculating number of dimes required for change if (dimes < coin10) //if less dimes are available, then we take all of them coin10 = dimes; amount = amount - 10 * coin10; //reducing amount by the 10*coin10 coin5 = amount / 5; //calculating number of nickels required for change if (nickels < coin5) //if less nickels are available, then we dont have sufficient change { cout << " Insufficient changes! "; cout << "Your transaction cannot be processed. "; cout << "Please take back your dollar bill. "; } else //otherwise, we display the change { amount = 25 * coin25 + 10 * coin10 + 5 * coin5; //recalculating amount cout << "Your change of " << amount << " cents is given as: quarter(s):" << coin25; cout << " dime(s):" << coin10; cout << " nickel(s):" << coin5 << endl << endl; //reducing the total number of coins by the total number of coins given quarters = quarters - coin25; dimes = dimes - coin10; nickels = nickels - coin5; cnt++; //incrementing the valid transactions by 1 } return 0;

}

void finalReport(int cnt, int& quarters, int& dimes, int& nickels) //Function to generate final report { cout << "Final report is being generated ... "; cout << "There are " << cnt << " valid transactions. "; //displaying total valid transactions //displaying total dollars-bills and coins in machine cout << "Number of dollars : " << cnt; cout << " Number of quarters: " << quarters; cout << " Number of dimes: " << dimes; cout << " Number of nickels: " << nickels << endl; float amount; //For calculating total amount in machine amount = 1.00*cnt + 0.25*quarters + 0.10*dimes + 0.05*nickels; cout << "Machine balance : $" << amount << endl; //Displaying total amount in machine cout << "Machine is shutting down. Good bye."; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!