Program using C. A vending machine sells three kinds of candy, Airheads for 25 c
ID: 3843332 • Letter: P
Question
Program using C.
A vending machine sells three kinds of candy, Airheads for 25 cents, Fun Dip for 40 cents, and Gummi Bears for 20 cents. Write a program that will simulate this vending machine. First, accept coins from the user (user enters #of cents as integers 5, 10, or 25), until the user enters zero. Then give the user a choice (with menu) for candies. If the user enters an invalid choice, display a “Invalid selection” message. If the amount deposited is sufficient, print the selection and calculate and display the change. If the amount deposited is not sufficient, display a message for insufficient amount and return the money.
Down below are the cases of how it should look when complied and run.
echo '===================================================='
./a.out <<-EndOfInput
10
10
10
0
1
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Insert coins: 10'
echo 'Insert coins: 10'
echo 'Insert coins: 10'
echo 'Insert coins: 0'
echo 'Enter your choice: 1'
echo 'Candy dispensing'
echo 'Your change is 5 cents'
echo '===================================================='
./a.out <<-EndOfInput
10
5
0
1
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Insert coins: 10'
echo 'Insert coins: 5'
echo 'Insert coins: 0'
echo 'Enter your choice: 1'
echo 'Insufficient amount, 15 cents returned'
echo '===================================================='
./a.out <<-EndOfInput
25
25
0
2
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Insert coins: 25'
echo 'Insert coins: 25'
echo 'Insert coins: 0'
echo 'Enter your choice: 2'
echo 'Candy dispensing'
echo 'Your change is 10 cents'
echo '===================================================='
./a.out <<-EndOfInput
10
10
10
0
4
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Insert coins: 10'
echo 'Insert coins: 10'
echo 'Insert coins: 10'
echo 'Insert coins: 0'
echo 'Enter your choice: 4'
echo 'Invalid choice, 30 cents returned'
echo '===================================================='
./a.out <<-EndOfInput
25
0
3
EndOfInput
echo '----------------------------------------------------'
echo 'Expected:'
echo 'Insert coins:25'
echo 'Insert coins: 0'
echo 'Enter your choice: 3'
echo 'Candy dispensing'
echo 'Your change is 5 cents'
echo '====================================================
Explanation / Answer
Code:
#include <stdio.h>
/* Program to simulate the vending machine functionality */
/* function prototype */
void chkbalance(int, int);
int main()
{
int a[20], i=0, j;
int input, choice, total_amt=0;
printf(" ============================== ");
/* picking coins amount user enters into an array */
while(input != 0){
scanf("%d", &input);
a[i++]=input;
total_amt+=input;
}
/* displaying the amount user enetered */
printf(" Expected:");
for(j = 0; j < i; j++){
printf(" Insert coins: %d", a[j]);
}
/* showing the candy menu so user can choose the candy interested */
printf(" Candy Menu");
printf(" ==========");
printf(" 1. Airheads - 25 cents");
printf(" 2. Fun Dip - 40 cents");
printf(" 3. Gummi Bears - 20 cents");
printf(" Enter you choice of candy(1/2/3)? ");
scanf("%d", &choice);
/* checking if choice if between the valid options 1 to 3 */
if(choice >=1 && choice <=3){
if(choice == 1)
chkbalance(total_amt, 25);
else if(choice == 2)
chkbalance(total_amt, 40);
else
chkbalance(total_amt, 20);
}
/* if choice is invalid we will display the below message */
else{
printf(" Invalid choice, %d cents returned", total_amt);
}
printf(" ================================= ");
return 0;
}
/* Method to validate if user entered sum is sufficient or insufficient for the candies available */
void chkbalance(int usr_input, int candy_amt)
{
if(usr_input > candy_amt){
printf(" Candy is dispensing");
printf(" Your change is %d cents", usr_input - candy_amt);
}
else if(usr_input < candy_amt){
printf(" Insufficient amount, %d cents returned", usr_input);
}
}
NOTE: I have added the Candy Menu as well in the code than what is shown in expected output. Because user will know what to choose from the candy options available
Execution and output:
186590cb0725:C bonkv$ ./a.out
==============================
10
10
10
0
Expected:
Insert coins: 10
Insert coins: 10
Insert coins: 10
Insert coins: 0
Candy Menu
==========
1. Airheads - 25 cents
2. Fun Dip - 40 cents
3. Gummi Bears - 20 cents
Enter you choice of candy(1/2/3)? 1
Candy is dispensing
Your change is 5 cents
=================================
186590cb0725:C bonkv$ ./a.out
==============================
10
5
0
Expected:
Insert coins: 10
Insert coins: 5
Insert coins: 0
Candy Menu
==========
1. Airheads - 25 cents
2. Fun Dip - 40 cents
3. Gummi Bears - 20 cents
Enter you choice of candy(1/2/3)? 1
Insufficient amount, 15 cents returned
=================================
186590cb0725:C bonkv$ ./a.out
==============================
25
25
0
Expected:
Insert coins: 25
Insert coins: 25
Insert coins: 0
Candy Menu
==========
1. Airheads - 25 cents
2. Fun Dip - 40 cents
3. Gummi Bears - 20 cents
Enter you choice of candy(1/2/3)? 2
Candy is dispensing
Your change is 10 cents
=================================
186590cb0725:C bonkv$ ./a.out
==============================
10
10
10
0
Expected:
Insert coins: 10
Insert coins: 10
Insert coins: 10
Insert coins: 0
Candy Menu
==========
1. Airheads - 25 cents
2. Fun Dip - 40 cents
3. Gummi Bears - 20 cents
Enter you choice of candy(1/2/3)? 4
Invalid choice, 30 cents returned
=================================
186590cb0725:C bonkv$ ./a.out
==============================
25
0
Expected:
Insert coins: 25
Insert coins: 0
Candy Menu
==========
1. Airheads - 25 cents
2. Fun Dip - 40 cents
3. Gummi Bears - 20 cents
Enter you choice of candy(1/2/3)? 3
Candy is dispensing
Your change is 5 cents
=================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.