469,621 Members | 1,988 Online

# program to simulate a soft drink machine

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.
Dec 10 '06 #1
4 9303
horace1
1,510 Expert 1GB
you cannot assign to an array element so
Expand|Select|Wrap|Line Numbers
1. Drink[0]= {"coke", .75, 20};
2.
you can initialise the array elements when you define it
Expand|Select|Wrap|Line Numbers
1. struct Machine
2. {
3. string Drink_name;
4. float cost;
5. int Num_of_drinks;
6. };
7.
8. struct Machine Drink[]={{"coke", .75, 20},{"A and W", .75, 20},
9.                         {"sprite", .75, 20},{"shasta", .80, 20},
10.                         {"minute maid", .80, 20}};
11.
Dec 10 '06 #2
you cannot assign to an array element so
Expand|Select|Wrap|Line Numbers
1. Drink[0]= {"coke", .75, 20};
2.
you can initialise the array elements when you define it
Expand|Select|Wrap|Line Numbers
1. struct Machine
2. {
3. string Drink_name;
4. float cost;
5. int Num_of_drinks;
6. };
7.
8. struct Machine Drink[]={{"coke", .75, 20},{"A and W", .75, 20},
9.                         {"sprite", .75, 20},{"shasta", .80, 20},
10.                         {"minute maid", .80, 20}};
11.
QUESTION: i ask the user to input how much money they will enter into the machine, then am suppose to give back to them the change or if they didnt put enough money give back how much to put in.

first i did a loop for the menu choice. then i asked the user to pick a drink.
if they enter 1 they picked coke. but what i dont know is how to use the amount the user inputs and subract the cost of coke from this.

can i say.....
if (moneyinput < .75)
cout << .75 - moneyinput << endl

to give back change to the user, can i say.......
if (moneyinput > .75)
cout << moneyinput - .75 << endl

i dont think i can do this but how else can i access the cost of coke that is in my array?
Dec 10 '06 #3
DeMan
1,806 1GB
Drink[0].cost; //for coke

Don't forget to check Drink[0].num_of_Drinks (to make sure you have drink there)
Dec 10 '06 #4
THIS IS WHAT I HAVE SO FAR................

#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()
{
float moneyin;
float moneyout;
float temp;
{
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;
{
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: i the user doesnt put in enough money, i have to tell them the right amount to put in that is the difference of drink[0] - moneyin. i have to do this until they put in the right amount. so if they need to put in 50cents they only put .15 i have to tell them to put in .35 cents, if the only put in 10, i have to tell them to put in 25cents until the right amount is reached. i tried a while loop but i think am doing it wrong. please offer some guidance.
Dec 11 '06 #5