program to simulate a soft drink machine

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • queenma7
    New Member
    • Oct 2006
    • 16

    #1

    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.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you cannot assign to an array element so
    Code:
    Drink[0]= {"coke", .75, 20};
    you can initialise the array elements when you define it
    Code:
    struct Machine
    {
    string Drink_name;
    float cost;
    int Num_of_drinks;
    };
    
    struct Machine Drink[]={{"coke", .75, 20},{"A and W", .75, 20},
                            {"sprite", .75, 20},{"shasta", .80, 20},
                            {"minute maid", .80, 20}};

    Comment

    • queenma7
      New Member
      • Oct 2006
      • 16

      #3
      Originally posted by horace1
      you cannot assign to an array element so
      Code:
      Drink[0]= {"coke", .75, 20};
      you can initialise the array elements when you define it
      Code:
      struct Machine
      {
      string Drink_name;
      float cost;
      int Num_of_drinks;
      };
      
      struct Machine Drink[]={{"coke", .75, 20},{"A and W", .75, 20},
                              {"sprite", .75, 20},{"shasta", .80, 20},
                              {"minute maid", .80, 20}};
      thanks that was helpful.
      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?

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Drink[0].cost; //for coke

        Don't forget to check Drink[0].num_of_Drinks (to make sure you have drink there)

        Comment

        • queenma7
          New Member
          • Oct 2006
          • 16

          #5
          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()
          {
          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: 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.

          Comment

          Working...