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.

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

hullo,

Which bit are you stuck on.

>>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 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 :

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.

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()
{
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

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

while (number is invalid) {
keep getting input;
}

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:

while (moneyin < drink[menu_item].cost) {
// keep asking for correct amount
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.