Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Chetan Grewal
- Lab 4
- Description:
- */
- #include<iostream>
- #include<string>
- #include<iomanip>
- using namespace std;
- // Sets up struct
- struct drinkInfo{
- string name;
- double price;
- int quantity;
- };
- //Money calculation function
- void moneyCalc(double &due, double &total);
- int main(){
- drinkInfo drinks[5]; // Makes array for the struct
- int choice = 0; // Drink option user selects
- double due;
- double totalPaid = 0;
- // Set values of all the drinks
- drinks[0] = {"Cola", .75, 5};
- drinks[1] = {"Root Beer", .75, 20};
- drinks[2] = {"Lemon-Lime", .75, 20};
- drinks[3] = {"Grape Soda", .80, 20};
- drinks[4] = {"Cream Soda", .80, 20};
- // Loop runs as long as option 6 isn't selected
- while(choice !=6){
- cout << "\n\n";
- for(int i=0; i<5; i++){ //Display Options
- cout << (i+1) << ") " << drinks[i].name << "\t$";
- cout << fixed << setprecision(2) << drinks[i].price << "\t";
- cout << drinks[i].quantity << "\n";
- }
- cout << "6) ";
- cout << "Leave the drink machine\n";
- // Ask user to choose drink or quit
- cout << "\nPlease choice an option: ";
- cin >> choice;
- // Display error if invald choice
- while(choice <1 || choice > 6){
- cout << "\nError, please enter a valid choice: ";
- cin >> choice;
- }
- // Switch executes similar program for various drinks
- switch(choice){
- case 1 :
- // Displays error if the drink quantity is 0
- if(drinks[0].quantity == 0){
- cout << drinks[0].name << " is out of stock\n\n";
- break; // Breaks out to let user pick different drink
- }
- due = drinks[0].price; // Sets drink price to a variable
- moneyCalc(due, totalPaid); // Money calculation program
- totalPaid += due; // Adds money earned to total
- drinks[0].quantity -= 1; // Takes 1 drink out of total avaible
- break; // Breaks out of switch when done
- // Code works the same, but for different drink for cases 2-5
- case 2:
- if(drinks[1].quantity == 0){
- cout << drinks[1].name << " is out of stock\n\n";
- break;
- }
- due = drinks[1].price;
- moneyCalc(due, totalPaid);
- totalPaid += due;
- drinks[1].quantity -= 1;
- break;
- case 3:
- if(drinks[2].quantity == 0){
- cout << drinks[2].name << " is out of stock\n\n";
- break;
- }
- due = drinks[2].price;
- moneyCalc(due, totalPaid);
- totalPaid += due;
- drinks[2].quantity -= 1;
- break;
- case 4:
- if(drinks[3].quantity == 0){
- cout << drinks[3].name << " is out of stock\n\n";
- break;
- }
- due = drinks[3].price;
- moneyCalc(due, totalPaid);
- totalPaid += due;
- drinks[3].quantity -= 1;
- break;
- case 5:
- if(drinks[4].quantity == 0){
- cout << drinks[4].name << " is out of stock\n\n";
- break;
- }
- due = drinks[4].price;
- moneyCalc(due, totalPaid);
- totalPaid += due;
- drinks[4].quantity -= 1;
- break;
- // Breaks out of switch to exit
- case 6:
- break;
- }
- }
- // Display total earned
- cout << "Total money earned this session: " << totalPaid;
- cout << "\n\n";
- return 0;
- }
- void moneyCalc(double &due, double &total){
- bool paid = 0;
- double payment;
- double change;
- while(paid==0){
- if(due==0){
- paid = 1;
- }
- // Ask user for money
- cout << "\nThe drink costs $" << due << ".";
- cout << "\nPlease insert money for drink: ";
- cin >> payment;
- // Display error if wrong amount entered
- while(payment < 0 || payment > 1){
- cout << "\nError, enter amount between $0.00 and $1.00: ";
- cin >> payment;
- }
- // If payment is over the amount due
- // user recieves change
- if(payment > due){
- change = payment - due;
- cout << "\nYour change is " << change;
- due = 0;
- // If the payment is exact, no change recieved
- } else{
- due -= payment;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement