| | |
program to simulate a soft drink machine. PLease help!!!
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2006
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,760
Reputation:
Solved Threads: 283
>>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!
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.
•
•
•
•
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.
@queenma7
The proper way to initialize struct variables is :
C++ Syntax (Toggle Plain Text)
typedef struct { string name ; int count ; } Drink ; int main( ) { Drink aDrink = { "cola", 20 } ; // one way Drink arr_drink[2] = { { "cola", 3}, {"pepsi", 5} } ; // another way arr_drink[0] = { "cola", 3 } ; // error, won't work // third way of doing it for( int i = 0 ; i < 2; ++i ) { arr_drink[i].name = "" ; arr_drink[i].count = 0 ; } return 0 ; }
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
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
•
•
Join Date: Dec 2006
Posts: 2
Reputation:
Solved Threads: 0
WHAT I HAVE SO FAR..............
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
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <cmath> using namespace std; struct Machine { string Drink_name; float cost; int num_drinks; }; struct Machine drink[] = {{"coke", .75, 20}, {"A and W", .75, 20}, { "sprite", .75, 20}, {"shasta", .80, 20}, {"minute maid", .80, 20}}; int main() { int menu_choice; float moneyin; float moneyout; float temp; while (menu_choice != 6) { cout <<"which drink would you like to buy:" << endl; cout <<"1-coke" << endl; cout <<"2-A and W" << endl; cout <<"3-sprite" << endl; cout <<"4-shasta" << endl; cout <<"5-minute maid" << endl; cout <<"6-Quit\n " << endl; cin >> menu_choice; if (menu_choice == 1) { cout << "enter money to inserted into machine between 0 and $1.00" << endl; cin >> moneyin; while (moneyin < drink[0].cost) { cout <<"enter" << (drink[0].cost<< endl; cin >> moneyout; temp = if (temp == drink[0].cost) cout <<"great" << endl; } if (moneyin > drink[0].cost) cout <<" your change is:" << (moneyin - drink[0].cost) << endl; } } return 0; }
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
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
When asking the user to enter a specific amount, reference the cost by doing something like
Just to make sure, this is incorrect, isn't it?
The expression is going to have a hard time changing if you're only modifying
Hope this helps
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
C++ Syntax (Toggle Plain Text)
while (number is invalid) { keep getting input; }
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: C++ Syntax (Toggle Plain Text)
while (moneyin < drink[menu_item].cost) { // keep asking for correct amount }
while (moneyin < drink[0].cost)
{
cout <<"enter" << drink[0].cost<< endl; // (
cin >> moneyout;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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Program for simulate all page replacement algorithms (C)
- drink machine (C++)
- Program using a Menu Selection with Subprograms (C++)
- Vending Machine (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help finding bug in maze designer code
- Next Thread: Help On Looping
Views: 2390 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return sort stream string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






