Reference no: EM132208493
Question :
Write a program in C that simulates a soft drink machine. The program should use a structure that stores the following data:
Drink Name
Drink Cost
Number of Drinks in the Machine
The program should create an array of structures (maximum 20). The elements should be initialized by reading all data from a file called "drinks.txt". The first line in the data file contains the number of drink records. See sample data below.
Each time a program runs, it should load the data from the file and enter into a loop that displays the list of drinks and their cost on the screen. If the user selects a drink, he or she will next enter the amount of money that is to be inserted into the machine.
The program should display the amount of change that would be returned and subtract one from the number of drinks 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 total amount of money the machine earned and save all the data back into the input file "drinks.txt". Next time the program is run, it will load the new data into the array.
Input Validation: When the user enters an amount of money, do not accept negative values or values greater that $1.00.
Sample Data File (the first entry is the number of soft drinks):
7
Coca Cola
.75
2
Root Beer
.75
5
Grape Soda
.80
20
Cream Soda
.80
18
Mocha Frappuccino
1.00
10
Diet Sprite
.75
1
Nestea Iced Tea
.85
5
Sample Run:
Note that the menu options will change depending on the number of drinks stored in the input file.
1. Coca Cola 0.75
2. Root Beer 0.75
3. Grape Soda 0.80
4. Cream Soda 0.80
5. Mocha Frappuccino 1.00
6. Diet Sprite 0.75
7. Nestea Iced Tea 0.85
8. Quit
Enter your choice:1
Enter the amount of money:1.00
Amount of change: 0.25
1. Coca Cola 0.75
2. Root Beer 0.75
3. Grape Soda 0.80
4. Cream Soda 0.80
5. Mocha Frappuccino 1.00
6. Diet Sprite 0.75
7. Nestea Iced Tea 0.85
8. Quit
Enter your choice: 2
Enter the amount of money: 5.00
Illegal amount
1. Coca Cola 0.75
2. Root Beer 0.75
3. Grape Soda 0.80
4. Cream Soda 0.80
5. Mocha Frappuccino 1.00
6. Diet Sprite 0.75
7. Nestea Iced Tea 0.85
8. Quit
Enter your choice: 8
Total collected: $0.75