Language C Is there a way to do this using an input file Fo
Language: C // Is there a way to do this using an input file?
For instance: Here is a sample input file that contains the information for a Drink Machine:
8
Coke 1.25 25
Root-beer 1.25 20
Mountain-Dew 1.25 25
Water 1.00 40
Energy 2.00 5
Iced-tea 1.25 35
Lemonade 1.30 15
Iced-Coffee 2.00 35
Please explain with comments. Thanks.
13. Drink Machine Simulator 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 Drinks in Machine Programming Challenges 655 The program should create an array of five structures. The elements should beialized with the following data: Drink Name Cost Cola .75 Root Beer .75 Number in Machine 20 20 20 20 20 Lemon-Lime .75 Grape Soda.80 Cream Soda .8o 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, 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 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 Input Validation: When the user enters an amount of money, do not accept negative values or values greater than S1.00Solution
// C code Drink machine simulator
#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
struct Machine
 {
     char name[50];
     double cost;
     int number;
 };
// calculate change for the drink
 void userPayment(double drinkCost)
 {
     double amountPaid;
     printf(\"Drink cost: $%0.2lf\ \",drinkCost);
     printf(\"Enter payment amount: \");
     scanf(\"%lf\",&amountPaid);
    while(amountPaid < 0 || amountPaid < drinkCost)
     {
         printf(\"Invalid Amount!\ \");
         printf(\"Enter payment: \");
         scanf(\"%lf\",&amountPaid);
     }
     printf(\"Change returned: $%0.2lf\ \ \",(amountPaid-drinkCost));
 }
 //
 int displayMenu(struct Machine d[], int total)
 {
     int userChoice = total,i;
     bool sold = true;
    while(1)
     {
         printf(\"\ Menu\  Drink\\t\\tCost\\tleft\ \");
         for(i=0;i<total;i++)
         {
             printf(\"%d.%-10s\\t\",(i+1),d[i].name);
             printf(\"%0.2lf\\t%d\ \",d[i].cost,d[i].number);
         }
         printf(\"%d.Exit\ \",i+1);
         printf(\"Enter userChoice \");
         scanf(\"%d\",&userChoice);
       
         if(userChoice<1||userChoice>(total+1))
             printf(\"Invalid Input\ \ \");
         else if(d[userChoice-1].number==0)
         {
             printf(\"Item Sold out\ \ \");
             return -1;
         }
         else
             break;
       
     }
     return userChoice-1;
 }
int main()
 {
    FILE *fptr;
     if ((fptr = fopen(\"drinkmachine.txt\", \"r\")) == NULL)
     {
         printf(\"Error! opening file\");
         // Program exits if file pointer returns NULL.
         return 0;  
     }
    int size;
     fscanf(fptr,\"%d\", &size);
     int total = size;
     int i = 0;
     struct Machine drink[size];
    while(size--)
     {
         fscanf(fptr,\"%s\", drink[i].name);
         fscanf(fptr,\"%lf\", &drink[i].cost);
         fscanf(fptr,\"%d\", &drink[i].number);
         i++;
     }
fclose(fptr);
    int userChoice;
     double moneyEarned=0;
    while(1)
     {
         userChoice=displayMenu(drink, total);
         if(userChoice == (total))
             break;
         if(userChoice == -1)
             continue;
         // calulate money earned by machine
         userPayment(drink[userChoice].cost);
         moneyEarned= moneyEarned + drink[userChoice].cost;
         drink[userChoice].number--;
   
     }
    printf(\"\ Money earned by machine: $%0.2lf\ \",moneyEarned);
     return 0;
 }
/*
drinkmachine.txt
 8
 Coke 1.25 25
 Root-beer 1.25 20
 Mountain-Dew 1.25 25
 Water 1.00 40
 Energy 2.00 5
 Iced-tea 1.25 35
 Lemonade 1.30 15
 Iced-Coffee 2.00 35
 output:
Menu
 Drink      Cost    left
 1.Coke          1.25    25
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    5
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    35
 9.Exit
 Enter userChoice 1
 Drink cost: $1.25
 Enter payment amount: 1
 Invalid Amount!
 Enter payment: 2.35
 Change returned: $1.10
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    5
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    35
 9.Exit
 Enter userChoice 5
 Drink cost: $2.00
 Enter payment amount: 5
 Change returned: $3.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    4
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    35
 9.Exit
 Enter userChoice 5
 Drink cost: $2.00
 Enter payment amount: 2
 Change returned: $0.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    3
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    35
 9.Exit
 Enter userChoice 5
 Drink cost: $2.00
 Enter payment amount: 2
 Change returned: $0.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    2
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    35
 9.Exit
 Enter userChoice 8
 Drink cost: $2.00
 Enter payment amount: 3
 Change returned: $1.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    2
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    34
 9.Exit
 Enter userChoice 5
 Drink cost: $2.00
 Enter payment amount: 2
 Change returned: $0.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    1
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    34
 9.Exit
 Enter userChoice 5
 Drink cost: $2.00
 Enter payment amount: 2
 Change returned: $0.00
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    0
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    34
 9.Exit
 Enter userChoice 5
 Item Sold out
 Menu
 Drink      Cost    left
 1.Coke          1.25    24
 2.Root-beer     1.25    20
 3.Mountain-Dew 1.25    25
 4.Water         1.00    40
 5.Energy        2.00    0
 6.Iced-tea      1.25    35
 7.Lemonade      1.30    15
 8.Iced-Coffee   2.00    34
 9.Exit
 Enter userChoice 9
Money earned by machine: $13.25
*/





