program to simulate a soft drink machine. PLease help!!!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2006
Posts: 2
Reputation: queenma7 is an unknown quantity at this point 
Solved Threads: 0
queenma7 queenma7 is offline Offline
Newbie Poster

program to simulate a soft drink machine. PLease help!!!

 
0
  #1
Dec 10th, 2006
Hi everyone, am new to this site. I have been reading through the forums and it has been helpful for me so far. I have an assignment that could make or break my grade. so please help! am not asking for it to be done for me, but i just need some input as i work through it. thanks so much.

Write a program that simulates a soft drink machine. The program should use a structure that stores the following information:

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
Coke .75 20
A_and_W .75 20
Sprite .75 20
Shasta .80 20
MinuteMaid .80 20

Each time the program runs, it should enter a loop that does the following:

Display a list of drinks on the screen
Allow the user to either quit or pick a drink
If the user picks a drink, he or shill will then enter the amount of money to be inserted into the machine
The program should then display the amount of change that would be returned and update the number of drinks still in machine. If the user has not entered enough money, the program should display how much more should be entered to complete the sale.
If the user selects a drink that has been sold it, a message should be displayed

This loop should continue until the user says Quit. When the user quits, it should display the total amount of money earned by that machine (based on how many drinks were bought).

The program should not accept money amounts less than 0 or greater than $1.00





this is what i have so far....

struc Machine
{
string Drink_name;
int cost;
int Num_of_drinks;
};

struct Machine Drink[4];

drink[0]= {"coke", .75, 20};
drink[1]= {"A and W", .75, 20};
drink[2]={"sprite", .75, 20};
drink[3] ={"shasta", .80, 20};
drink[4]= {"minute maid", .80, 20};

QUESTION: i dont know if am initializing the variables correctly. so if someone could please provide some insight on this. thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: program to simulate a soft drink machine. PLease help!!!

 
0
  #2
Dec 10th, 2006
hullo,

Which bit are you stuck on.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,760
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: program to simulate a soft drink machine. PLease help!!!

 
0
  #3
Dec 10th, 2006
>>QUESTION: i dont know if am initializing the variables correctly.

The ability to answer this question on your own is part of the process called debugging. It's a skill you're going to need to prosper in this endeavor so you might as well learn how to do it early.

The only way to know what information is stored in a variable is to view it. There are three ways you can do that, that I know of.

1) You can use a debugger, which is a software program that allows you to follow the value of a variable through each step of the program.

2) You can write the value of a variable at some point in the program to a file and view it there.

3) You can write the values to the screen and view it there.

If you elect to use version 2 or 3 then you often will need to remove the code that allowed you to debug the program and leave behind only the desired portion of the program. Not infrequently the amount of debugging code your remove can be as much as what's left behind.

So, dive in. Either use a debugger or print out the value of a variable someplace to see if it contains the value you thought it did.

Good luck!

Here are some initial thoughts:
1) it's struct not struc, but maybe leaving off the terminal t is a typo

2) This: struct Machine Drink[4]; I believe would be correct in C but you don't need the keyword struct here in C++, so telling us which language you are using would be helpful

3) You are trying to put five Machines in an array that only holds four. Oops!
Last edited by Lerner; Dec 10th, 2006 at 3:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: program to simulate a soft drink machine. PLease help!!!

 
0
  #4
Dec 10th, 2006
Originally Posted by Lerner View Post
The ability to answer this question on your own is part of the process called debugging. It's a skill you're going to need to prosper in this endeavor so you might as well learn how to do it early.
The code won't even compile so how does the question of "debugging" come into the picture ?

@queenma7

The proper way to initialize struct variables is :
  1. typedef struct
  2. {
  3. string name ;
  4. int count ;
  5. } Drink ;
  6.  
  7. int main( )
  8. {
  9. Drink aDrink = { "cola", 20 } ; // one way
  10.  
  11. Drink arr_drink[2] = { { "cola", 3}, {"pepsi", 5} } ; // another way
  12.  
  13. arr_drink[0] = { "cola", 3 } ; // error, won't work
  14.  
  15. // third way of doing it
  16. for( int i = 0 ; i < 2; ++i )
  17. {
  18. arr_drink[i].name = "" ;
  19. arr_drink[i].count = 0 ;
  20. }
  21. return 0 ;
  22. }

Hope it helped, bye.
Last edited by ~s.o.s~; Dec 10th, 2006 at 3:44 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2
Reputation: queenma7 is an unknown quantity at this point 
Solved Threads: 0
queenma7 queenma7 is offline Offline
Newbie Poster

Re: program to simulate a soft drink machine. PLease help!!!

 
0
  #5
Dec 10th, 2006
WHAT I HAVE SO FAR..............
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <cmath>
  5. using namespace std;
  6. struct Machine
  7. {
  8. string Drink_name;
  9. float cost;
  10. int num_drinks;
  11. };
  12. struct Machine drink[] = {{"coke", .75, 20}, {"A and W", .75, 20}, { "sprite", .75, 20},
  13. {"shasta", .80, 20}, {"minute maid", .80, 20}};
  14. int main()
  15. {
  16. int menu_choice;
  17. float moneyin;
  18. float moneyout;
  19. float temp;
  20. while (menu_choice != 6)
  21. {
  22. cout <<"which drink would you like to buy:" << endl;
  23. cout <<"1-coke" << endl;
  24. cout <<"2-A and W" << endl;
  25. cout <<"3-sprite" << endl;
  26. cout <<"4-shasta" << endl;
  27. cout <<"5-minute maid" << endl;
  28. cout <<"6-Quit\n " << endl;
  29. cin >> menu_choice;
  30. if (menu_choice == 1)
  31. {
  32. cout << "enter money to inserted into machine between 0 and $1.00" << endl;
  33. cin >> moneyin;
  34. while (moneyin < drink[0].cost)
  35. {
  36. cout <<"enter" << (drink[0].cost<< endl;
  37. cin >> moneyout;
  38. temp =
  39. if (temp == drink[0].cost)
  40. cout <<"great" << endl;
  41. }
  42. if (moneyin > drink[0].cost)
  43. cout <<" your change is:" << (moneyin - drink[0].cost) << endl;
  44. }
  45. }
  46. return 0;
  47. }


Question: if the user doesnt not inout enough money, am suppose to tell them how much to put in and keep doing this until the right amount i tried a while loop but i keep getting an infinite loop. i dont know what else to do. please comment. thanks
Last edited by WaltP; Dec 11th, 2006 at 3:13 am. Reason: See the BBCODE info in the announcements at the top of the forum
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: program to simulate a soft drink machine. PLease help!!!

 
0
  #6
Dec 10th, 2006
Please use code tags. See my signature for more information.

You're taking the long-way approach to this problem when you come to the point of getting the user to pay. First of all, you'll need to validate the menu choice. You don't want the user entering some random number that will cause memory faults. Use something like
  1. while (number is invalid) {
  2. keep getting input;
  3. }
When asking the user to enter a specific amount, reference the cost by doing something like drink[menu_choice].cost instead of all those ugly if()s and then manually specifying each element. There's almost always a more efficient way. To make sure the user inputs a valid amount, do the same way you validated your menu choice:
  1. while (moneyin < drink[menu_item].cost) {
  2. // keep asking for correct amount
  3. }
Just to make sure, this is incorrect, isn't it?
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost<< endl; // (
cin >> moneyout;
The expression is going to have a hard time changing if you're only modifying moneyout.

Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2390 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC