How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask ATLProg Your Own Question

ATLProg
ATLProg, Software Engineer
Category: Homework
Satisfied Customers: 8788
Experience:  Expert in Java C++ C C# VB Javascript Design SQL HTML
44910485
Type Your Homework Question Here...
ATLProg is online now
A new question is answered every 9 seconds

Exercise 7: Drink Machine SimulatorThe purpose of this

Customer Question

Exercise 7: Drink Machine Simulator

The purpose of this exercise is to give you practice with abstract data types, namely structures and arrays of structures.

Write a program that simulates a soft drink machine. The program should use a structure that stores the following data:
• Drink Name
• Drink Cost
• Number of Drinks in Machine

The program should create an array of five structures. The elements should be initialized with the following data:

Drink Name Cost Number in Machine
Coca-Cola .75 20
Root Beer .75 20
Sprite .75 20
Spring Water .80 20
Apple Juice .95 20

Please see the input file ("DrinkMachineInventory.txt"). Each time the program runs, it should read the data from the input file and then enter a loop that performs the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed. The loop then repeats. When the user chooses to quit the program it should display the total amount of money the machine earned.

Input Validation: Only accept positive values for the amount of money. Also, do not accept values greater than $1.00.

Your code should be modular (use functions) and pass variables (by value/by reference) where appropriate. Also, use named constants where appropriate.

Include your input file and your source code (.cpp)
Submitted: 1 year ago.
Category: Homework
Expert:  ATLProg replied 1 year ago.
Hello Welcome. Thank you for your question, I’m ATLProg and I’ll try to help you.

How soon do you need this answer ?
Customer: replied 1 year ago.

I need it by tuesday, i got all this now.


 


#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;


double machineTotal = 0.0;
int userChoice = 0;


void displayMenu();



int main()
{


cout << "Welcome to the soda machine!" << endl;
displayMenu();


}


void displayMenu()
{
int userChoice = 0;
int cokeLeft = 20;
int rBLeft = 20;
int spriteLeft = 20;
int sWLeft = 20;
int aJLeft = 20;
int cokeMin = 0;
int rBMin = 0;
int spriteMin = 0;
int sWMin = 0;
int aJMin = 0;



cout << "Thank you, XXXXX XXXXX your selection!" <<endl;
cout << endl;
cout << "0) Coca-Cola 0.75 " << cokeLeft << endl;
cout << "1) Root Beer 0.75 " << rBLeft << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
if (userChoice < 0 || userChoice > 5)
{
cin.clear();
cin.ignore(100, '\n');
cout << "Enter a valid choice: ";
cout << endl;
}


 


while(userChoice != 5)
{
if(userChoice == 0)
{
cout << "Thump, thump, splat!" <<endl;
cout << "Enjoy your beverage!" << endl << endl;
cokeLeft--;
if (cokeLeft <= cokeMin)
{
cout << "We ran out of that drink, please pick another one." << endl;
cout << "Thank you, XXXXX XXXXX your selection!" <<endl;
cout << endl;
cout << "0) Coca-Cola 0.75 0 " << endl;
cout << "1) Root Beer 0.75 " << rBLeft << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
if (userChoice == 0)
{
cin.clear();
cin.ignore(100, '\n');
cout << "Enter a valid choice: ";
cout << endl;
}
do
{
if (userChoice == 0)
{
cout << "We ran out of that drink, please pick another one." << endl;
cout << "Thank you, XXXXX XXXXX your selection!" << endl;
cout << endl;
cout << "0) Coca-Cola 0.75 0 " << endl;
cout << "1) Root Beer 0.75 " << rBLeft << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
if (userChoice == 0)
{
cin.clear();
cin.ignore(100, '\n');
cout << "Enter a valid choice: ";
cout << endl;
}
}
cout << endl;
}while(userChoice != 0);


}
}



else if (userChoice == 1)
{
cout << "Thump, thump, splat!" <<endl;
cout << "Enjoy your beverage!" << endl << endl;
rBLeft--;
if (rBLeft <= rBMin)
{
cout << "We ran out of that drink, please pick another one." << endl;
cout << "Thank you, XXXXX XXXXX your selection!" <<endl;
cout << endl;
cout << "0) Coca-Cola 0.75 " << cokeLeft << endl;
cout << "1) Root Beer 0.75 0" << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
cin.clear();
cin.ignore(100, '\n');
cout << "Enter a valid choice: ";
do
{
if (userChoice == 1)
{
cout << "We ran out of that drink, please pick another one." << endl;
cout << "Thank you, XXXXX XXXXX your selection!" <<endl;
cout << endl;
cout << "0) Coca-Cola 0.75 " << cokeLeft << endl;
cout << "1) Root Beer 0.75 0" << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
cout << endl;
}
}while(userChoice == 1);
}
}


else if (userChoice == 2)
{
cout << "Thump, thump, splat!" <<endl;
cout << "Enjoy your beverage!" << endl << endl;
spriteLeft--;
if (spriteLeft <= spriteMin)
{
cout << "We ran out of that drink, please pick another one." << endl;
}
}


else if (userChoice == 3)
{
cout << "Thump, thump, splat!" <<endl;
cout << "Enjoy your beverage!" << endl << endl;
sWLeft--;
if (sWLeft <= sWMin)
{
cout << "We ran out of that drink, please pick another one." << endl;
}
}


else if (userChoice == 4)
{
cout << "Thump, thump, splat!" <<endl;
cout << "Enjoy your beverage!" << endl << endl;
aJLeft--;
if (aJLeft <= aJMin)
{
cout << "We ran out of that drink, please pick another one." << endl;
}
}


else if (userChoice == 5)
{
cout << "You're now exiting the soda machine" << endl;
}


else
{
cout << "Enter a valid number" << endl;
}


cout << "Thank you, XXXXX XXXXX your selection!" <<endl;
cout << endl;
cout << "0) Coca-Cola 0.75 " << cokeLeft << endl;
cout << "1) Root Beer 0.75 " << rBLeft << endl;
cout << "2) Sprite 0.75 " << spriteLeft << endl;
cout << "3) Spring Water 0.80 " << sWLeft << endl;
cout << "4) Apple Juice 0.95 " << aJLeft << endl;
cout << "5) Leave the drink machine" << endl;
cout << endl;
cout << "Choose one: ";
cin >> userChoice;
cout << endl;



}


 


}


 


 


 

Expert:  ATLProg replied 1 year ago.
Download Answer Link I hope I’ve provided the information you were seeking. If you are happy with my service, please provide a rating. If not, please let me know so l can continue to help you. Thank you.
Customer: replied 1 year ago.

That' s the same exact code my classmate has, the whole thing needs to be edited a bit. You did it today for him.

Expert:  ATLProg replied 1 year ago.
so what would you like to see edited ? Variable names etc ?
Customer: replied 1 year ago.

It basically needs new variables, and change the functions a little bit, for example the way you calculate the costs.

Expert:  ATLProg replied 1 year ago.
Sure, check back in 10 mins pls
Customer: replied 1 year ago.

Alright thanks!

Expert:  ATLProg replied 1 year ago.
Download Answer Link I hope I’ve provided the information you were seeking. If you are happy with my service, please provide a rating. If not, please let me know so l can continue to help you. Thank you.
Please start your question text with "For ATLProg Only" to request me again, next time you need help!
http://www.justanswer.com/homework/expert-atlprog/ - Please bookmark this link to ask new question.
Expert:  ATLProg replied 1 year ago.
Are we all good ?
Customer: replied 1 year ago.

int i=0;
string currentword= "";
char currentChar;



while(!inventoryFile.eof())
{
currentword= "";
currentChar = inventoryFile.get();
while(true)
{
if(isdigit(currentChar) || currentChar == '\n')
break;
else
currentword += currentChar;
currentChar = inventoryFile.get();
}


if(currentword != "")
{
d.drinkName = currentword;
inventoryFile >> d.drinkCost >> d.drinkNumber ;
i++;
}
}


 


 



Can you explain to me what's happening here?


I don't get what currentword variable is doing

Expert:  ATLProg replied 1 year ago.
it gets the name of currrent drink's name...IN our text file we have drink names and we are reading them until a user encounters a digit or a line break
Customer: replied 1 year ago.

On the part where you have to pay, I need a valid input.


 


I changed it up a bit but it works the same


 


 


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cstdlib>


using namespace std;


struct Machine
{
string drinkName;
double drinkCost;
int drinkNumber;
};


void readFileIntoArray (Machine []);
int displayMenu (Machine[]);
void printMenu (Machine drinks[]);
void getPaymentFromUser (double);



int main()
{
Machine drink[5];
int userChoice;
double moneyEarned = 0;


readFileIntoArray(drink);
userChoice = displayMenu(drink);
while(userChoice != 5)
{
getPaymentFromUser(drink[userChoice].drinkCost);
moneyEarned+=drink[userChoice].drinkCost;
drink[userChoice].drinkNumber--;
userChoice = displayMenu(drink);
}
cout << "Today the machine has made $" <<setprecision(2) << fixed << moneyEarned << endl;
return 0;
}



//Display the Menu
int displayMenu (Machine drinks[])
{
int userchoice = 8, i;
bool isSoldout = true;
while((userchoice < 1 || userchoice > 6) || isSoldout)
{
isSoldout = false;
printMenu (drinks);
cin >> userchoice;
if(userchoice < 1 || userchoice > 6)
{
cin.clear();
cin.ignore(100, '\n');
cout << "invalid entry" << endl;
}


else
if( drinks[userchoice-1].drinkNumber == 0)
{
cout << "sold out" << endl;
isSoldout = true;
}
}
return userchoice-1;
}


void printMenu(Machine drinks[])
{
cout << "Menu\n";
cout << " Drink Cost\tleft\t" << endl;
for(int i = 0; i < 5; i++)
{
cout << i + 1 << ") " << setw(15) << left << drinks.drinkName << setw(5);
cout << setprecision(2) << fixed << drinks.drinkCost << "\t" <<drinks.drinkNumber << endl;
}
cout << "6. Leave the drink machine" << endl;
cout << endl;
cout <<"Choose one: ";
}
//Get the payment
void getPaymentFromUser(double p)
{
double paymentFromUser;
cout << "Enter an amount of money: ";
cin >> paymentFromUser;
cout << endl;
cout << "Thump, thump, thump, splat!" << endl;
cout << "Enjoy your beverage" << endl;
cout << endl;
while(paymentFromUser < 0 || paymentFromUser > 1.0 || paymentFromUser < p)
{
cout << "Please insert the correct amount for your drink!" << endl;
cout << "Maximum payment is $1.00" << endl;
cout << "Enter payment: ";
cin >> paymentFromUser;
if(paymentFromUser < 0 || paymentFromUser > 1.0 || paymentFromUser < p)
{
cin.clear();
cin.ignore(100, '\n');
cout << "invalid entry\n";
}


}
cout << "Your change is: $" << setprecision(2) << fixed << paymentFromUser - p << endl;
cout << endl;
return;
}


void readFileIntoArray(Machine d[])
{
ifstream inputFile("DrinkMachineInventory.txt");
if(inputFile.fail())
{
cout << "Could not find the file DrinkMachineInventory.txt" << endl;
cout << "Exiting the program" << endl;
exit(0);
}


int i=0;
string currentword= "";
char currentChar;



while(!inputFile.eof())
{
currentword= "";
currentChar = inputFile.get();
while(true)
{
if(isdigit(currentChar) || currentChar == '\n')
break;
else
currentword += currentChar;
currentChar = inputFile.get();
}


if(currentword != "")
{
d.drinkName = currentword;
inputFile >> d.drinkCost >> d.drinkNumber ;
i++;
}
}


inputFile.close();
}


 

Expert:  ATLProg replied 1 year ago.
Validations were already there, not sure I understand the need to modify code..
Customer: replied 1 year ago.

I'm trying to post a picture but i cant

Expert:  ATLProg replied 1 year ago.
I ahve no idea why you are running into issues, the last guy didn't have any
Customer: replied 1 year ago.

Because I'm trying to have my code impeccable, my professor is very picky on the grading.


 


It's just when I have to input the money and let's say I type a letter it says


"Please enter the correct amount for your drink"


"Maximum payment is $1.00"


Enter payment: invalid entry


"Please enter the correct amount for your drink"


"Maximum payment is $1.00"


Enter payment:


 


 


double paymentFromUser;
cout << "Enter an amount of money: ";
cin >> paymentFromUser;
cout << endl;
while(paymentFromUser < 0 || paymentFromUser > 1.0 || paymentFromUser < p)
{
cout << "Please insert the correct amount for your drink!" << endl;
cout << "Maximum payment is $1.00" << endl;
cout << "Enter payment: ";
cin >> paymentFromUser;
if(paymentFromUser < 0 || paymentFromUser > 1.0 || paymentFromUser < p)
{
cin.clear();
cin.ignore(100, '\n');
cout << "invalid entry\n";
}


 


I fixed it so it doesnt go on an endless loop like before you gave it to me. It's just that, he tests everything, i'm sorry man.

Expert:  ATLProg replied 1 year ago.
Did you modify any code from what I gave for this method ?
Customer: replied 1 year ago.

Barely, I took out a few of the couts here and there.

Expert:  ATLProg replied 1 year ago.
ok...let me see
Expert:  ATLProg replied 1 year ago.
whats the input that you want to validate and whats the expected result
Customer: replied 1 year ago.
I want it to say just invalid entry or something along those lines
Expert:  ATLProg replied 1 year ago.
So you just want to display a different message than what it is displaying right now !? Thats it ?
Customer: replied 1 year ago.
No man, I would ask help for that, If you check out my mag from 9:10 that's what happens when I enter a char instead of an int
Expert:  ATLProg replied 1 year ago.
ok, so the case you are worried about is when a user enters say 'a' instead of '1' ...correct ?
Customer: replied 1 year ago.
Yes, it looks like it loops through it twice. That's the only thing and I'd be done
Expert:  ATLProg replied 1 year ago.
ok..stay here
Customer: replied 1 year ago.
Okay
Expert:  ATLProg replied 1 year ago.
ok...how much time do we have ?
Customer: replied 1 year ago.

Until tomorrow before 6 PM.

Expert:  ATLProg replied 1 year ago.
ok..thx
Expert:  ATLProg replied 1 year ago.
try this file please - Download Answer Link I hope I’ve provided the information you were seeking. If you are happy with my service, please provide a rating. If not, please let me know so l can continue to help you. Thank you.
Customer: replied 1 year ago.

http://www.freeimagehosting.net/6r3eh

Expert:  ATLProg replied 1 year ago.
yep..fixinign it
Customer: replied 1 year ago.

You're going to be on today right?

Expert:  ATLProg replied 1 year ago.
yes..
Customer: replied 1 year ago.

Is the code going to be fixed?

Expert:  ATLProg replied 1 year ago.
yes
Customer: replied 1 year ago.

Is it going to take long? I only have 30 minutes left

Expert:  ATLProg replied 1 year ago.
Sorry I was out, did you still need it or are we past the deadline now ?
Customer: replied 1 year ago.

Yep, we past it.

Expert:  ATLProg replied 1 year ago.
Ok Sorry for the trouble
Expert:  ATLProg replied 1 year ago.
If the answer was helpful and it provided a good grade, please do not forget to come back and rate answer
Customer: replied 1 year ago.

I got a good grade, thank you so much!

Expert:  ATLProg replied 1 year ago.
THIS ANSWER IS LOCKED!
You can view this answer by clicking here to Register or Login and paying $3.
If you've already paid for this answer, simply Login.
ATLProg, Software Engineer
Category: Homework
Satisfied Customers: 8788
Experience: Expert in Java C++ C C# VB Javascript Design SQL HTML
ATLProg and 2 other Homework Specialists are ready to help you
Expert:  ATLProg replied 1 year ago.

Please start your question text with "For ATLProg Only" to request me again, next time you need help!
http://www.justanswer.com/homework/expert-atlprog/ - Please bookmark this link to ask new question.

JustAnswer in the News:

 
 
 
Ask-a-doc Web sites: If you've got a quick question, you can try to get an answer from sites that say they have various specialists on hand to give quick answers... Justanswer.com.
JustAnswer.com...has seen a spike since October in legal questions from readers about layoffs, unemployment and severance.
Web sites like justanswer.com/legal
...leave nothing to chance.
Traffic on JustAnswer rose 14 percent...and had nearly 400,000 page views in 30 days...inquiries related to stress, high blood pressure, drinking and heart pain jumped 33 percent.
Tory Johnson, GMA Workplace Contributor, discusses work-from-home jobs, such as JustAnswer in which verified Experts answer people’s questions.
I will tell you that...the things you have to go through to be an Expert are quite rigorous.
 
 
 

What Customers are Saying:

 
 
 
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
< Last | Next >
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C. Freshfield, Liverpool, UK
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex Los Angeles, CA
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP Hesperia, CA
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin Kernersville, NC
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther Woodstock, NY
  • Thank you so much for taking your time and knowledge to support my concerns. Not only did you answer my questions, you even took it a step further with replying with more pertinent information I needed to know. Robin Elkton, Maryland
  • He answered my question promptly and gave me accurate, detailed information. If all of your experts are half as good, you have a great thing going here. Diane Dallas, TX
 
 
 

Meet The Experts:

 
 
 
  • Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
< Last | Next >
  • http://ww2.justanswer.com/uploads/BU/BusinessTutor/2012-2-2_115741_Kouki2.64x64.jpg Manal Elkhoshkhany's Avatar

    Manal Elkhoshkhany

    Tutor

    Satisfied Customers:

    4522
    More than 5000 online tutoring sessions.
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Engineer

    Satisfied Customers:

    3458
    Expert in Java C++ C C# VB Javascript Design SQL HTML
  • http://ww2.justanswer.com/uploads/LI/lindaus/2012-6-10_04811_IMG20120609164157.64x64.jpg Linda_us's Avatar

    Linda_us

    Finance, Accounts & Homework Tutor

    Satisfied Customers:

    3124
    Post Graduate Diploma in Management (MBA)
  • http://ww2.justanswer.com/uploads/chooser77/2009-08-18_162025_Chris.jpg Chris M.'s Avatar

    Chris M.

    M.S.W. Social Work

    Satisfied Customers:

    2385
    Master's Degree, strong math and writing skills, experience in one-on-one tutoring (college English)
  • http://ww2.justanswer.com/uploads/JawaadAhmed/2009-6-27_12137_SIs_SHadi.jpg F. Naz's Avatar

    F. Naz

    Chartered Accountant

    Satisfied Customers:

    1988
    Experience with chartered accountancy
  • http://ww2.justanswer.com/uploads/JK/jkcpa/2011-1-16_182614_jkcpa.64x64.jpg Bizhelp's Avatar

    Bizhelp

    CPA

    Satisfied Customers:

    1876
    Bachelors Degree and CPA with Accounting work experience
  • http://ww2.justanswer.com/uploads/avremote/photoa.jpg Seanna's Avatar

    Seanna

    Tutor

    Satisfied Customers:

    1781
    3,000+ satisfied customers, all topics, A+ work
 
 
 

Related Homework Questions

Chat Now With A Tutor
ATLProg
ATLProg
Sr Software Engineer
1216 Satisfied Customers
Expert in Java C++ C C# VB Javascript Design SQL HTML