22F-7465 Lab#4 Hometask

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

#include <iostream>

#include <string>
using namespace std;
struct Drink {
string name;
double cost;
int num;
};
int main() {
Drink drinks[4] = {
{"Cola", 0.75, 20},
{"Root Beer", 0.75, 20},
{"Grape Soda", 0.80, 20},
{"Cream Soda", 0.80, 20}
};
double earnings = 0.0;
cout << "Drinks Available: " << endl;
for (int i = 0; i < 4; i++) {
cout << i+1 << ". " << drinks[i].name << " - $" << drinks[i].cost << endl;
}
cout << "Enter the number of the drink you want to buy (1-4), or 0 to quit: ";
int choice;
cin >> choice;
if (choice == 0) {
cout << "Total earnings: $" << earnings << endl;
} else if (choice < 1 || choice > 4) {
cout << "Invalid choice, please try again." << endl;
} else if (drinks[choice-1].num == 0) {
cout << "Sorry, " << drinks[choice-1].name << " is sold out." << endl;
} else {
double money;
cout << "Please insert $" << drinks[choice-1].cost << ": ";
cin >> money;
if (money < 1.0) {
cout << "Please insert at least $1.00." << endl;
} else {
double change = money - drinks[choice-1].cost;
cout << "Thank you! Your change is $" << change << "." << endl;
earnings += drinks[choice-1].cost;
drinks[choice-1].num--;
}
}
return 0;
}

You might also like