SAMPLE OUTPUT! 1. Coke (can) 2. Coke (bottle) 3. Mountain Dew (can) 4. Water 5.
ID: 3714050 • Letter: S
Question
SAMPLE OUTPUT!
1. Coke (can)
2. Coke (bottle)
3. Mountain Dew (can)
4. Water
5. Full Throttle
6. Quit the Program
Enter your choice: 3
The cost is: $0.75
1. Coke (can)
2. Coke (bottle)
3. Mountain Dew (can)
4. Water
5. Full Throttle
6. Quit the Program
Enter your choice: 5
The cost is: $2.00
1. Coke (can)
2. Coke (bottle)
3. Mountain Dew (can)
4. Water
5. Full Throttle
6. Quit the Program
Enter your choice: 6
Units Remaining:
Coke (can) 6
Coke (bottle) 8
Mountain Dew (can) 7
Water 8
Full Throttle 7
Amount earned: $4.25
??Please use C++ for beginner!
Write a program that simulates a soft drink machine. The program should use a structure that stores the following data Drink Name Drink Cost Number of units currently in the machine The program should create an array of tive structures. The elements should be initialized with the following data Drink Name Cost 0.75 1.25 0.75 1.25 2.00 Number in Machine Coke (can) Coke (bottle) Mountain Dew (can) Water Full Throttle 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 a drink (that has not sold out), the program should display the cost of the drink and subtract one from the number of units of that drink left in the machine. If the user selects a drink that has sold out, a message should be displayed. The loop then repeats. When the user chooses to quit the program it should display the number of units of each drink left in the machine and the total amount of money the machine earnedExplanation / Answer
#include <string>
#include <iostream>
using namespace std;
const size_t NO_OF_UNITS = 8;
const size_t NO_OF_DRINKS = 5;
struct Drink {
string drinkName;
double drinkCost;
size_t noOfUnits;
Drink(string name, double price, size_t units)
{
drinkName = name;
drinkCost = price;
noOfUnits = units;
}
};
void displayTheUnitsRemaining(Drink drinks[]) {
cout << "Units Remaining:" << endl;
for (size_t i = 0; i < NO_OF_DRINKS; i++)
{
cout << drinks[i].drinkName << " " << drinks[i].noOfUnits << endl;
}
}
void processUserChoice(Drink drinks[], int choice) {
Drink& drink = drinks[choice - 1];
if (drink.noOfUnits != 0) {
cout << "The cost is: $" << drink.drinkCost;
drink.noOfUnits--;
}
else {
cout << "Sorry, the drink " << drink.drinkName << " has been sold out.";
}
}
int showAndGetChoice(Drink drinks[]) {
cout << endl << "List of Drinks:" << endl;
for (size_t i = 0; i < NO_OF_DRINKS; i++)
{
cout << i + 1 << ". " << drinks[i].drinkName << endl;
}
cout << "6. Quit the Program" << endl << endl;
cout << "Enter your choice: ";
int choice;
cin >> choice;
if (choice == 6)
{
displayTheUnitsRemaining(drinks);
return 0;
}
else
{
processUserChoice(drinks, choice);
return 1;
}
}
double calculateEarnings(Drink drinks[]) {
double earnings = 0;
for (size_t i = 0; i < NO_OF_DRINKS; i++)
{
earnings += drinks[i].drinkCost * (NO_OF_UNITS - drinks[i].noOfUnits);
}
return earnings;
}
int main()
{
Drink drinks[NO_OF_DRINKS] = {
Drink("Coke (can)", 0.75, NO_OF_UNITS),
Drink("Coke (bottle)", 1.25, NO_OF_UNITS),
Drink("Mountain Dew (can)", 0.75, NO_OF_UNITS),
Drink("Water", 1.25, NO_OF_UNITS),
Drink("Full Throttle", 2.0, NO_OF_UNITS)
};
while (showAndGetChoice(drinks))
{
}
cout << "Amount earned: $" << calculateEarnings(drinks) << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.