0

I need help to write this program I am fairly new to C and C++. Please help me get started. I don't know where to start but I'm trying my best. I need to write a vending machine program.

  1. Display a list of drinks on the screen
  2. Allow the user to either quit or pick a drink
  3. If the user picks a drink, he or she will then enter the amount of money to be inserted into the machine
#include<stdio.h>
#define system

void menu(void);
void chocolate(void);
void price(void);
void change(void);
void end(void);


int choc[10]={0,1,2,3,4,5,6,7,8,9};


int main()
{
 int intro;
 int option;
 int choice;
 int money;
 int payment;

e: menu();

 printf("Enter Your Option: ");
 scanf("%d",&intro);
 printf("\n");

 if(intro==1)
 {

w:  printf("The Chocolates\n\n");
  printf("1.Choose Chocolate\n");
  printf("2.View A Price To Be Paid\n");
  printf("3.Back To Main Menu\n\n");

  printf("Rules: Machine can only receive RM1 and RM5 only! \n\n\n");

  printf("Enter Your Option: ");
  scanf("%d",&option);
  printf("\n\n");

  if(option==1)
  {
   system("cls");
   printf("Name list of chocolate: ");
   printf("\n\n");
   printf("1. Cadburry Black Forest");          printf("\tRM3.00\n");
   printf("2. Kitkat");                         printf("\t\t\tRM2.00\n");
   printf("3. Crispy");                         printf("\t\t\tRM2.00\n");
   printf("4. Crunch");                         printf("\t\t\tRM2.00\n");
   printf("5. Mars");                           printf("\t\t\tRM2.00\n");
   printf("6. Kinder Bueno");                   printf("\t\tRM2.00\n");
   printf("7. White Kinder Bueno");             printf("\t\tRM2.00\n");
   printf("8. Milky Bar");                      printf("\t\t\tRM2.00\n");
   printf("9. M&M");                            printf("\t\t\t\tRM2.00\n");
   printf("10. Choki Choki");                    printf("\t\t\tRM2.00\n");
   printf("\n\n");

   printf("Choose Chocolate: ");
   scanf("%d",&choice);
   printf("\n\n");

   switch(choice)
   {
    case 0 :
     chocolate();
     printf("You choose Cadburry Black Forest\n\n");
     goto w;
    case 1 :
     chocolate();
     printf("You choose Kitkat\n\n");
     goto w;
    case 2 :
     chocolate();
     printf("You choose Crispy\n\n");
     goto w;
    case 3 :
     chocolate();
     printf("You choose Crunch\n\n");
     goto w;
    case 4 :
     chocolate();
     printf("You choose Mars\n\n");
     goto w;
    case 5 :
     chocolate();
     printf("You choose Kinder Bueno\n\n");
     goto w;
    case 6 :
     chocolate();
     printf("You choose White Kinder Bueno\n\n");
     goto w;
    case 7 :
     chocolate();
     printf("You choose Milky Bar\n\n");
     goto w;
    case 8 :
     chocolate();
     printf("You choose M&M\n\n");
     goto w;
    case 9 :
     chocolate();
     printf("You choose Choki Choki\n\n");
     goto w;

    default : goto e;
   }
  }
  else if(option==2)
  {
   printf("View A Price To Be Paid: ");
   scanf("%d",&choice);
   printf("\n\n");

   switch(choice)
   {
    case 0 :
     price();
     printf("RM3.00\n\n");
     goto w;
    case 1 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 2 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 3 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 4 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 5 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 6 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 7 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 8 :
     price();
     printf("RM2.00\n\n");
     goto w;
    case 9 :
     price();
     printf("RM2.00\n\n");
     goto w;

     default : goto e;
   }
  }
  else
   goto e;
 }

 else if(intro==2)
 {
  printf("Enter your money: ");
  scanf("%d",&money);
  printf("\n\n");

   if(choice==0)
   {
    printf("Your payment is RM3");
    printf("\n\n");
    choc[0]=payment;
    goto e;
   }
   if(choice==1)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[1]=payment;
    goto e;
   }
   if(choice==2)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[2]=payment;
    goto e;
   }
   if(choice==3)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[3]=payment;
    goto e;
   }
   if(choice==4)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[4]=payment;
    goto e;
   }
   if(choice==5)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[5]=payment;
    goto e;
   }
   if(choice==6)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[6]=payment;
    goto e;
   }
   if(choice==7)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[7]=payment;
    goto e;
   }
   if(choice==8)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[8]=payment;
    goto e;
   }
   if(choice==9)
   {
    printf("Your payment is RM2");
    printf("\n\n");
    choc[9]=payment;
    goto e;
   }
   else
   {
    printf("ERROR!\n\n");
    goto e;
   }
 }

 else if(intro==3)
 {
 change();
 goto e;
 }

 else if(intro==4)
 {
  printf("Enjoy it!");

   if(choice==0)
   {
    printf("Thank you and enjoy your Cadburry Black Forest!");
    printf("\n\n");
    choc[0]=0;
    goto e;
   }
   if(choice==1)
   {
    printf("Thank you and enjoy your Kitkat!");
    printf("\n\n");
    choc[1]=0;
    goto e;
   }
   if(choice==2)
   {
    printf("Thank you and enjoy your Crispy!");
    printf("\n\n");
    choc[2]=0;
    goto e;
   }
   if(choice==3)
   {
    printf("Thank you and enjoy your Crunch!");
    printf("\n\n");
    choc[3]=0;
    goto e;
   }
   if(choice==4)
   {
    printf("Thank you and enjoy your Mars!");
    printf("\n\n");
    choc[4]=0;
    goto e;
   }
   if(choice==5)
   {
    printf("Thank you and enjoy your Kinder Bueno!");
    printf("\n\n");
    choc[5]=0;
    goto e;
   }
   if(choice==6)
   {
    printf("Thank you and enjoy your White Kinder Bueno!");
    printf("\n\n");
    choc[6]=0;
    goto e;
   }
   if(choice==7)
   {
    printf("Thank you and enjoy your Milky Bar!");
    printf("\n\n");
    choc[7]=0;
    goto e;
   }
   if(choice==8)
   {
    printf("Thank you and enjoy your M&M!");
    printf("\n\n");
    choc[8]=0;
    goto e;
   }
   if(choice==9)
   {
    printf("Thank you and enjoy your Choki Choki!");
    printf("\n\n");
    choc[9]=0;
    goto e;
   }
   else
   {
    printf("Error! Try Again.\n\n");
    goto e;
   }
 }
  else
  end();

}

void menu(void)
{
 printf("|WELCOME TO CHOCOLATES VENDING MACHINE|\n\n");

 printf("1.The Chocolates\n");
 printf("2.Login To Your Account\n");
 printf("3.View Change\n");
 printf("4.Exit Vending Machine\n");
 printf("5.Exit Program\n\n");
}

void chocolate(void)
{
 int u[30];
 char o[30];
 FILE *choclot;
 choclot=fopen("parking.txt","a");
 printf("Your Chocolate: ");
 scanf("%s",&o);
 printf("Total Price: ");
 scanf("%d",&u);
 fprintf(choclot,"Chocolate: %s Total Price: %d\n\n",o,u);
 fclose(choclot);
}

void change(void)
{
 int i;
 printf("Your Balance: \n\n");
 for(i=0;i<10;i++)
  printf("%d",choc[i]);
  printf("\n\n\n\n");
}

void end(void)
{
 system("cls");
 printf("\t\t\tTHANK YOU FOR USING THIS SERVICES\n");
  printf("\t\t\t\t  Visit Us At \n\n");
  printf("\t\t\t    vendingmachine.com.my\n\n\n");
  printf(" First Programmer : Adawiyah\n");
  printf(" First Programmer : Vijayah Santhi\n");
} 

Is it my program correct? Does my program neat and orderly? The goto statement is it that bad? Sorry, my English is bad. I'm trying my best finish my project within this few days. I need to present to my lecturer next week.

Sorry for troubling you. I don't know how to copy&paste here.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • RE: `goto` see http://stackoverflow.com/questions/46586/goto-still-considered-harmful – Tushar Mar 30 '13 at 05:12
  • You won't get a useful answer with a question like this. You need to find a specific thing in your program you are having trouble with, describe it and *then* someone will help you. – Hunter McMillen Mar 30 '13 at 05:13
  • One piece of advice I'd like to give you: instead of the huge switch statement, use an array as a lookup table for the sake of readability and separation of data and code. –  Mar 30 '13 at 05:21
  • your program is correct if it meets the terms of your assignment. If you run it you should know if that's the case or not. "neat and orderly" depends on what your teacher wants to see... that could include naming conventions, whitespace, and other things we can't tell you. The `goto` should generally be avoided, there are cases where it makes since, but your program is not one of them. Please see the [FAQ] for asking better questions – Mike Mar 30 '13 at 05:21

1 Answers1

0

In your first attempt, your problem was that you were subtracting the number they chose from the amount of money they inputted, rather than the cost of the beverage they chose. You could have modif your program rather simply:

  1. Make a new variable of type float called cost.
  2. In each case, before the break, set cost to the cost of the beverage they selected.
  3. Rather than:

    change = money - choice;
    

    You'll want:

    change = money - cost;
    

The other problem you were unable to solve (show change if there's more money than necessary; show how much extra needed if there isn't) is rather simple. First, you can see if there's enough money:

if(money >= cost) {
    // There's enough money.
}else{
    // There's not enough money.
}

Then you can print appropriately:

if(money >= cost) {
    printf("Change: %.2f\n", money - cost);
}else{
    printf("You need RM%.2f more.\n", cost - money);
}

I have not had enough time to review your newest code, but your first try, while it could have some further improvements as suggested by other people in that thread, was not far off from where you needed it to be.

icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Thank you so much icktoofay. I already done with my project. With your help and others. Its really work. –  Mar 30 '13 at 15:41