home / study / engineering / computer science / questions and answers / write a
ID: 3819880 • Letter: H
Question
home / study / engineering / computer science / questions and answers / write a program in c(not c+) that simulates a soft ...
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: Write a program in C(not C+) that simulates a soft...
Bookmark
Write a program in C(not 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
Required functions:
1. Write a function that displays all drink choices
2. Write a function that removes the new-line character from the end of a string
3. Write a function that stores all the data back to the file
Explanation / Answer
Whole code is clear and self explanatory with comments. For better view and understanding copy the code and paste in code editor
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ColdDrink{
char drink_name[200];
float drink_cost;
int no;
};
void display_choices(struct ColdDrink softdrinks[], int n){ /*This method diplayes choices of drinks*/
int i=0;
for(i=0;i<n;i++){ /* print drinks as shown */
printf("%d. %s %0.2f ",i+1,softdrinks[i].drink_name,softdrinks[i].drink_cost);
}
char buff[]="Quit";
printf("%d %s ",n+1,buff); /* print quit option */
}
void remove_new_line(char buff[]){ /* This method removes new line character from the string */
int n=strlen(buff); /* get the length of the string */
if(buff[n-1]==' '){/* if this string has new line character at the end of the string */
buff[n-1]=''; /* replace new line character with strinf termination character */
}
}
void writeData(struct ColdDrink softdrinks[], int n){ /* this method writes data into the file */
FILE *fptr=fopen("drinks.txt","w"); /* open pointer to file*/
char line[200]; /* prepare buffer to write line */
sprintf(line,"%d ",n); /* convert integer n to string */
fputs(line,fptr); /* write to the file */
int i;
for(i=0;i<n;i++){
sprintf(line,"%s ",softdrinks[i].drink_name); /* get name in the line buffer */
fputs(line,fptr); /* write to the file */
sprintf(line,"%0.2f ",softdrinks[i].drink_cost); /* get cost in the line buffer */
fputs(line,fptr); /* write to the file */
sprintf(line,"%d ",softdrinks[i].no); /* get number of drinks in the line buffer */
fputs(line,fptr); /* write to the file */
}
fclose(fptr); /* close the file pointer */
}
int main(){
/*************************************************
Read Process
***************************************************/
FILE *fptr=fopen("drinks.txt","r"); /* open file pointer to drinks.txt*/
int n_softdrinks=0;
char line[200]; /* buffer for reading line from file */
fgets(line,200,fptr); /*read number of soft driks from first line*/
remove_new_line(line); /* remove new line character from the read line */
sscanf(line,"%d",&n_softdrinks); /* get the number of soft drinks */
struct ColdDrink softdrinks[n_softdrinks]; /* allocate array for softdrinks */
for(int i=0;i<n_softdrinks;i++){
fgets(line,200,fptr); /* read softdrink name */
remove_new_line(line); /* remove new line character */
strcpy(softdrinks[i].drink_name,line); /* get cold drink name */
fgets(line,200,fptr); /* read softdrink cost */
remove_new_line(line); /* remove new line character */
sscanf(line,"%f",&softdrinks[i].drink_cost); /* get cost in float */
fgets(line,200,fptr);
remove_new_line(line); /* remove new line character */
sscanf(line,"%d",&softdrinks[i].no); /* get number in int */
}
fclose(fptr); /* close the file pointer */
/*************************************************
Run Process
***************************************************/
float earnings=0; /* keep track of earnings*/
int total_drinks=0; /* total number of drinks*/
for(int i=0;i<n_softdrinks;i++) /* calculate total_drinks */
total_drinks=total_drinks+softdrinks[i].no;
while(total_drinks>0){ /* total_drinks are positive */
display_choices(softdrinks,n_softdrinks);
int input;
printf("Enter your choice: "); /* ask user for choice */
scanf("%d",&input); /* get input from the user */
input=input-1; /* adjust to zero based indexing */
if(input==n_softdrinks ){ /* input option is quit */
break; /* break the loop */
}
else if(softdrinks[input].no==0){ /* input option coldrink is sold out */
printf("Coldrink of your choice is SOLD OUT please try again "); /* display message*/
}else{
float cost=softdrinks[input].drink_cost; /* record drink cost */
printf("Enter the amount of money: " ); /* ask user for money */
float money=0;
scanf("%f",&money); /* get money input */
if(money<0 || money>1.00 || money<cost){ /* invalid inputs */
printf("Amount of money entered should be between $0 to $1.00 and should be greater than drink cost ");
continue; /* go to start of loop */
}
float change=money-cost;
printf("Amount of change: $%0.2f ",change); /* print the amount of change */
softdrinks[input].no=softdrinks[input].no-1; /* reduce number of soft drinks */
earnings=earnings+cost; /* add to earnings */
total_drinks=total_drinks-1; /* reduce 1 from total drinks */
}
}
/* when process is over */
printf("Total Collected: %0.2f ",earnings); /* display earnings */
writeData(softdrinks,n_softdrinks); /* write remaining data to the file */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.