I have setup a vending machine program but I need to use looping to keep asking the user for money and accepting until $1.00 has been entered. For example, if the user enters .25 it will ask them to enter in more money. If they have entered in .25 and they enter in another .25 it should show that their new total is .50. I wanted to make the entire main function loop so that once the transaction is completed, the program will ask if you want to purchase a drink (Y/N) and once Y(yes) is chosen the program would loop again and start over with 0.00 and will ask to insert money again. If N(no) should be selected, the loop should exit and an output statement of what has been purchased should show up.
Code:
#include <stdio.h>
main()
{
float fAnySelection = 1.00;
int iSelection = 0;
float f1Payment = 0.00;
float fChangeDue = 0.00;
printf("\nWelcome to Harry's Retro Soda Pop Vending Machine\n");
printf("\nPlease Enter $1.00 to view Various Soda Choices: ");
scanf("%f%", &fAnySelection);
if (fAnySelection < 1.00){ //$1 inserted is greater than any selection.
printf("\nInserting more money guarantees you a drink next time! "); //User has to insert more money to get a drink from vending machine
printf("Please Enter at the least $1.00: ");
scanf("%f%", &fAnySelection);}
if (fAnySelection >= 1.00){ //Any selection is greater than or equal to $1
printf("\nEnter 1. for Coca-Cola\n"); //Different types of soda's that the user gets a choice from. Some of these are still around but they have been since the 1970's or even earlier.
printf("\nEnter 2. for Pepsi-Cola\n");
printf("\nEnter 3. for RC Cola\n");
printf("\nEnter 4. for Diet Rite Cola\n");
printf("\nEnter 5. for Tab\n");
printf("\nEnter 6. for Big Red Soda\n");
printf("\nEnter 7. for Brookdale Orange Soda\n");
printf("\nEnter 8. for Fresca Soda\n");
printf("\nEnter 9. for Nehi Grape Soda\n");
printf("\nEnter 10. for Welch's Grape Soda\n");
printf("\nPlease Enter Your Selection:");
scanf("%d%", &iSelection);}
if (iSelection == 1) {
printf("\nThanks for your purchase,you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 2) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 3) {
printf("\nThanks for your purchase and enjoy. Ohh! and don't forget your drink!\n");
}
if (iSelection == 4) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n"); //Let's user know that the choice they have made was a good idea.
}
if (iSelection == 5) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 6) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 7) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 8) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 9) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection == 10) {
printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
}
if (iSelection < 11) { //User making a choice greater than number indicated is invalid
printf("\nThe Choice you have selected is currently invalid. ");
printf("Please Try Again: ");
scanf("%d%", &iSelection); }
fChangeDue=fAnySelection-1.00;{
if (fChangeDue>0.00);
printf("\nAmount of change owed to user $%.2f", fChangeDue);} //Shows change owed to customer, if any that is owed.
getchar();
}