Problem 1 Write a program that simulates a soft drink machin

Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data:

            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

Cola

0.75

20

Root Beer

0.75

20

Lemon-Lime

0.75

20

Grape Soda

0.80

20

Cream Soda

0.80

20

Each time the program runs, it should enter a loop that performs the following steps: A list of drinks is displayed on the screen. The user should be allowed to either quit the program or pick a drink. If the user selects the drink, he or she will next enter the amount of money that is to be inserted into the drink machine. The program should display the amount of change that would be returned and subtract one from the number of that drink left in the machine. If the user selects a drink that had sold out, a message should be displayed. Similarly, if the user does not specify enough money, a different message should be displayed (and the drink should not be dispensed). When the user chooses to quit the program, it should display the total amount of money the machine earned.

Here is my code, but it keeps timing out:

#include <iostream>
#include <string>
using namespace std;

struct Machine // creating the \"machine\" structure
{
   string drinkName;
   double drinkCost;
   int numofdrinks;
};

void find(int); // creates a prototype

const int TOTAL_MACHINES = 5; // initializes a constant value for our amount of sodas
double totalAmount = 0;

Machine machine [TOTAL_MACHINES] = { // we are creating and initializing the machine structures
   {\"(1) Cola \",      0.75, 20},
   {\"(2) RootBeer\",   0.75, 20},
   {\"(3) Lemon Lime\", 0.75, 20},
   {\"(4) Grape Soda\", 0.80, 20},
   {\"(5) Cream Soda\", 0.80, 20}};

   int main()// starts our main function && declares our variables
   {
       string drink;
       bool t = true;

       while (t)
       {
           cout << \"----------------------------------------------------\" << endl;
           cout << \"Drink Name           |Cost        |Number in Machine\" << endl;
           cout << \"----------------------------------------------------\" << endl;

           for (int index = 0; index < TOTAL_MACHINES; index++)
           {
               cout << machine[index].drinkName << \"      \"
                   << machine[index].drinkCost << \"      \"
                   << machine[index].numofdrinks << endl;
           }
           cout << endl;

           //prompt the user
           cout<< \"Enter the exact name of the drink you want or the number is associates with, or type quit:\" << endl;
           cin >> drink;

           // compare the users input with the machine drinks
           if(drink == \"Cola\" )
           {
               find(0);
           }
           else if(drink == \"RootBeer \" )
           {
               find(1);
           }
           else if(drink == \"LemonLime\")
           {
               find(2);
           }
           else if(drink == \"GrapeSoda\" )
           {
               find(3);
           }
           else if(drink == \"CreamSoda\" )
           {
               find(4);
           }
           else
           {
               t = false;
           }
       }
      
       cout<< endl;
       cout << \" The total amount of money that we have earned is : $\" << endl;
       cout << totalAmount << endl;
       cout<< endl;
          
           system(\"pause\");
           return 0;
   }
  
   void find (int idx)
   {
        double amount;
        double change;
      
        cout << endl;
        cout << \"Please ener the amount you are willing to sacrifice : $\" <<endl;
        cin >> amount;
      
        while (amount < 0 || amount > 1)
        {
            cout << endl;
            cout << \"Please enter an amount between $0-$1.00\" << endl;
            cin >> amount;
        }

        change = amount -machine[idx].drinkCost;
        cout << endl;
        cout << \" Your change is : $ \" << change;
        cout << endl;
      
        machine[idx].numofdrinks = machine[idx].numofdrinks-1;
      
        totalAmount = totalAmount + machine[idx].drinkCost;
   }

Drink Name

Cost

Number in Machine

Cola

0.75

20

Root Beer

0.75

20

Lemon-Lime

0.75

20

Grape Soda

0.80

20

Cream Soda

0.80

20

Solution

#include <iostream>
#include <string>
using namespace std;

struct Machine // creating the \"machine\" structure
{
   string drinkName;
   double drinkCost;
   int numofdrinks;
};

void find(int); // creates a prototype

const int TOTAL_MACHINES = 5; // initializes a constant value for our amount of sodas
double totalAmount = 0;

Machine machine [TOTAL_MACHINES] = { // we are creating and initializing the machine structures
   {\"(1) Cola \",      0.75, 20},
   {\"(2) RootBeer\",   0.75, 20},
   {\"(3) Lemon Lime\", 0.75, 20},
   {\"(4) Grape Soda\", 0.80, 20},
   {\"(5) Cream Soda\", 0.80, 20}};

   int main()// starts our main function && declares our variables
   {
       string drink;
       bool t = true;

       while (t)
       {
           cout << \"----------------------------------------------------\" << endl;
           cout << \"Drink Name           |Cost        |Number in Machine\" << endl;
           cout << \"----------------------------------------------------\" << endl;

           for (int index = 0; index < TOTAL_MACHINES; index++)
           {
               cout << machine[index].drinkName << \"      \"
                   << machine[index].drinkCost << \"      \"
                   << machine[index].numofdrinks << endl;
           }
           cout << endl;

           //prompt the user
           cout<< \"Enter the exact name of the drink you want or the number is associates with, or type quit:\" << endl;
           cin >> drink;

           // compare the users input with the machine drinks
           if(drink == \"Cola\" )
           {
               find(0);
           }
           else if(drink == \"RootBeer \" )
           {
               find(1);
           }
           else if(drink == \"LemonLime\")
           {
               find(2);
           }
           else if(drink == \"GrapeSoda\" )
           {
               find(3);
           }
           else if(drink == \"CreamSoda\" )
           {
               find(4);
           }
           else
           {
               t = false;
           }
       }
      
       cout<< endl;
       cout << \" The total amount of money that we have earned is : $\" << endl;
       cout << totalAmount << endl;
       cout<< endl;
          
           system(\"pause\");
           return 0;
   }
  
   void find (int idx)
   {
        double amount;
        double change;
      
        cout << endl;
        cout << \"Please ener the amount you are willing to sacrifice : $\" <<endl;
        cin >> amount;
      
        while (amount < 0 || amount > 1)
        {
            cout << endl;
            cout << \"Please enter an amount between $0-$1.00\" << endl;
            cin >> amount;
        }

        change = amount -machine[idx].drinkCost;
        cout << endl;
        cout << \" Your change is : $ \" << change;
        cout << endl;
      
        machine[idx].numofdrinks = machine[idx].numofdrinks-1;
      
        totalAmount = totalAmount + machine[idx].drinkCost;
   }

Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data: Drink Name Drink Cos
Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data: Drink Name Drink Cos
Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data: Drink Name Drink Cos
Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data: Drink Name Drink Cos
Problem 1. Write a program that simulates a soft drink machine. The program should use a structure Beverage that stores the following data: Drink Name Drink Cos

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site