Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Language Arm Assembly. Soft Drink Machine. Write a program which simulates the o

ID: 3870479 • Letter: L

Question

Language Arm Assembly.

Soft Drink Machine. Write a program which simulates the operation of a soft drink machine. The machine will dispense, upon reception of the correct amount of change of 55 cents, a choice of Coke, Sprite, Dr. Pepper, Diet Coke, or Mellow Yellow. The program should accept only nickels, dimes, quarters, and one dollar bills, and return any excess amount of change. Also allow the customer (user) to switch his or her mind on purchasing a soft drink. Currency belonging to the set of acceptable correct change includes nickels, dimes, quarters, and one dollar bills. Like the actions of a real soft drink machine, this program will be interactive. Input to the program will be read from the keyboard and output will be displayed to the screen. Prompt the user with an initial message describing the choices of soft drinks

Explanation / Answer

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

const int NUM_DRINKS = 5;  

int choice;                

bool productCheck;        

struct Drinks

{

const string name;     

double price;          

int qty;               

};

int userChoice(Drinks*);                       

void insertFunc(double*);                     

bool checkFunction(Drinks*, double*, int*);  

void changeFunction(double*, double*);         

void adminFuncitons(Drinks*);                  

int main()

{

double userCash;       

double changeDue;      

double profit = 0;     

cout << setprecision(2) << fixed;

Drinks machine[NUM_DRINKS] =

{

{“Cola     “, .75},

{“Root Beer”, .75},

{“Lemon-Lime”, .75},

{“Grape Soda”, .80},

{“Cream Soda”, .80}

};

Drinks *drinksPtr = machine;

ifstream fill;

fill.open(“inventory.txt”);

for (int i = 0; i < NUM_DRINKS; i++)

{

fill >> machine[i].qty;

}

while(choice!=6)

{

changeDue = 0;

userCash = 0;

cout << “Plase make a selection ”;

choice = userChoice(drinksPtr);

if(choice == 6 || choice == 7)

break;

cout << “ Please insert coins. Maximum of $1.00. “;

insertFunc(&userCash);

productCheck = checkFunction(drinksPtr, &userCash, &choice);

if(productCheck)

{

cout << “ thud! ”;

changeFunction(&userCash, &drinksPtr[choice].price);       

profit += drinksPtr[choice].price;                        

}

else

{

changeFunction(&userCash, &changeDue);

break;

}

}

ofstream inventory;                        

inventory.open(“inventory.txt”);      

for (int i = 0; i < NUM_DRINKS; i++)       

{

inventory << drinksPtr[i].qty << ” “;

}

inventory.close();                         

ofstream lastProfit;                      

lastProfit.open(“profit.txt”);             

lastProfit << “$” << profit;               

lastProfit.close();                        

cout << “The total sales of this transaction was $” << profit;

return 0;

}

int userChoice(Drinks *choice)

{

int selectionChoice;

do

{

cout <<

“# Drink name Price    Qty ”

“1. Cola       .75       ” << choice[0].qty <<

“ 2. Root Beer   .75       ” << choice[1].qty <<

“ 3. Lemon-Lime .75       ” << choice[2].qty <<

“ 4. Grape Soda .80       ” << choice[3].qty <<

“ 5. Cream Soad .80       ” << choice[4].qty <<

“ 6. Cancel Transaction”

“ 7. Vendor Company Functions”

“ Selection Choice: “;

cin >> selectionChoice;

if(selectionChoice == 6)

break;

else if(selectionChoice == 7)

{

adminFuncitons(choice);

break;

}

else if(choice[selectionChoice-1].qty == 0)

cout << “ Error – Please make another selection ”;

}while(choice[selectionChoice-1].qty == 0);

return selectionChoice;

void insertFunc (double *cash)

{

double cashIn;                     

do

{

cin >> cashIn;

if (cashIn < 0)

cout << “Error! – Please insert coins. Maximum of $1.00. “;

}while(cashIn < 0);

*cash+=cashIn;

if (*cash > 1)

{

double overage = *cash – 1;

changeFunction(cash, &overage);

}

}

bool checkFunction(Drinks *vend, double *cashIn, int *choiceCheck)

{

bool check, exit;

if (vend[*choiceCheck-1].qty != 0)

{

while(vend[*choiceCheck-1].price > *cashIn)

{

char response;

cout << “Insufficent funds. If you Would like to” <<

” insert more coins press Y else press any key. ”;

cin >> response;

if (toupper(response) == ‘Y’)

insertFunc(cashIn);

else

{

exit = true;

break;

}

}

if (exit == false)

{

cout << “Enjoy your drink! ”;

vend[*choiceCheck-1].qty = vend[*choiceCheck-1].qty – 1;   

check = true;                                              

}

else

check = false;                                            

}

else                                                               

{

cout << “Error – Please contact vendor at (123)-467-9991 ”;

check = false;                                                      

}

return check;

}

void changeFunction(double *cashIn, double *difference)

{

if (*cashIn > 1)

{

*cashIn = *cashIn – *difference;

cout << “Your change ammount of $” << *difference << ” is being returned. clink clink ”;

else

{

*cashIn = *cashIn – *difference;

cout << “Your change ammount of $” << *cashIn << ” is being returned. clink clink ”;

}

}

void adminFuncitons(Drinks *restock)

{

string password, userEntry;         

password = “Password”;             

bool exit = false;                

do{

cout << “To access admin funcitons please enter Password or type exit ”;

cin >> userEntry;

if(userEntry == “exit”)

{

exit = true;

cout << “ Leaving admin mode ”;

break;

}

}while(password != userEntry);

if(!exit)

{

for(int i=0; i<NUM_DRINKS; i++)

{

cout << “Please enter the quanity you wish to change for” << endl << restock[i].name << ” “;

cin >> restock[i].qty;

}

cout << “ Quantities will take effect upon restart. Have a nice day! ”;

}

}