自动售货机项目

Vending Machine Project

本文关键字:项目 自动售货机      更新时间:2023-10-16

我需要帮助来编写这个程序,我对C和c++相当陌生。请帮我开始吧。我不知道从哪里开始,但我在尽力。我需要写一个自动售货机程序。

  1. 在屏幕上显示饮料列表
  2. 允许用户退出或选择饮料
  3. 如果用户选择了饮料,他或她将输入要插入机器的金额
#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 Chocolatesnn");
  printf("1.Choose Chocolaten");
  printf("2.View A Price To Be Paidn");
  printf("3.Back To Main Menunn");
  printf("Rules: Machine can only receive RM1 and RM5 only! nnn");
  printf("Enter Your Option: ");
  scanf("%d",&option);
  printf("nn");
  if(option==1)
  {
   system("cls");
   printf("Name list of chocolate: ");
   printf("nn");
   printf("1. Cadburry Black Forest");          printf("tRM3.00n");
   printf("2. Kitkat");                         printf("tttRM2.00n");
   printf("3. Crispy");                         printf("tttRM2.00n");
   printf("4. Crunch");                         printf("tttRM2.00n");
   printf("5. Mars");                           printf("tttRM2.00n");
   printf("6. Kinder Bueno");                   printf("ttRM2.00n");
   printf("7. White Kinder Bueno");             printf("ttRM2.00n");
   printf("8. Milky Bar");                      printf("tttRM2.00n");
   printf("9. M&M");                            printf("ttttRM2.00n");
   printf("10. Choki Choki");                    printf("tttRM2.00n");
   printf("nn");
   printf("Choose Chocolate: ");
   scanf("%d",&choice);
   printf("nn");
   switch(choice)
   {
    case 0 :
     chocolate();
     printf("You choose Cadburry Black Forestnn");
     goto w;
    case 1 :
     chocolate();
     printf("You choose Kitkatnn");
     goto w;
    case 2 :
     chocolate();
     printf("You choose Crispynn");
     goto w;
    case 3 :
     chocolate();
     printf("You choose Crunchnn");
     goto w;
    case 4 :
     chocolate();
     printf("You choose Marsnn");
     goto w;
    case 5 :
     chocolate();
     printf("You choose Kinder Buenonn");
     goto w;
    case 6 :
     chocolate();
     printf("You choose White Kinder Buenonn");
     goto w;
    case 7 :
     chocolate();
     printf("You choose Milky Barnn");
     goto w;
    case 8 :
     chocolate();
     printf("You choose M&Mnn");
     goto w;
    case 9 :
     chocolate();
     printf("You choose Choki Chokinn");
     goto w;
    default : goto e;
   }
  }
  else if(option==2)
  {
   printf("View A Price To Be Paid: ");
   scanf("%d",&choice);
   printf("nn");
   switch(choice)
   {
    case 0 :
     price();
     printf("RM3.00nn");
     goto w;
    case 1 :
     price();
     printf("RM2.00nn");
     goto w;
    case 2 :
     price();
     printf("RM2.00nn");
     goto w;
    case 3 :
     price();
     printf("RM2.00nn");
     goto w;
    case 4 :
     price();
     printf("RM2.00nn");
     goto w;
    case 5 :
     price();
     printf("RM2.00nn");
     goto w;
    case 6 :
     price();
     printf("RM2.00nn");
     goto w;
    case 7 :
     price();
     printf("RM2.00nn");
     goto w;
    case 8 :
     price();
     printf("RM2.00nn");
     goto w;
    case 9 :
     price();
     printf("RM2.00nn");
     goto w;
     default : goto e;
   }
  }
  else
   goto e;
 }
 else if(intro==2)
 {
  printf("Enter your money: ");
  scanf("%d",&money);
  printf("nn");
   if(choice==0)
   {
    printf("Your payment is RM3");
    printf("nn");
    choc[0]=payment;
    goto e;
   }
   if(choice==1)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[1]=payment;
    goto e;
   }
   if(choice==2)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[2]=payment;
    goto e;
   }
   if(choice==3)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[3]=payment;
    goto e;
   }
   if(choice==4)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[4]=payment;
    goto e;
   }
   if(choice==5)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[5]=payment;
    goto e;
   }
   if(choice==6)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[6]=payment;
    goto e;
   }
   if(choice==7)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[7]=payment;
    goto e;
   }
   if(choice==8)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[8]=payment;
    goto e;
   }
   if(choice==9)
   {
    printf("Your payment is RM2");
    printf("nn");
    choc[9]=payment;
    goto e;
   }
   else
   {
    printf("ERROR!nn");
    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("nn");
    choc[0]=0;
    goto e;
   }
   if(choice==1)
   {
    printf("Thank you and enjoy your Kitkat!");
    printf("nn");
    choc[1]=0;
    goto e;
   }
   if(choice==2)
   {
    printf("Thank you and enjoy your Crispy!");
    printf("nn");
    choc[2]=0;
    goto e;
   }
   if(choice==3)
   {
    printf("Thank you and enjoy your Crunch!");
    printf("nn");
    choc[3]=0;
    goto e;
   }
   if(choice==4)
   {
    printf("Thank you and enjoy your Mars!");
    printf("nn");
    choc[4]=0;
    goto e;
   }
   if(choice==5)
   {
    printf("Thank you and enjoy your Kinder Bueno!");
    printf("nn");
    choc[5]=0;
    goto e;
   }
   if(choice==6)
   {
    printf("Thank you and enjoy your White Kinder Bueno!");
    printf("nn");
    choc[6]=0;
    goto e;
   }
   if(choice==7)
   {
    printf("Thank you and enjoy your Milky Bar!");
    printf("nn");
    choc[7]=0;
    goto e;
   }
   if(choice==8)
   {
    printf("Thank you and enjoy your M&M!");
    printf("nn");
    choc[8]=0;
    goto e;
   }
   if(choice==9)
   {
    printf("Thank you and enjoy your Choki Choki!");
    printf("nn");
    choc[9]=0;
    goto e;
   }
   else
   {
    printf("Error! Try Again.nn");
    goto e;
   }
 }
  else
  end();
}
void menu(void)
{
 printf("|WELCOME TO CHOCOLATES VENDING MACHINE|nn");
 printf("1.The Chocolatesn");
 printf("2.Login To Your Accountn");
 printf("3.View Changen");
 printf("4.Exit Vending Machinen");
 printf("5.Exit Programnn");
}
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: %dnn",o,u);
 fclose(choclot);
}
void change(void)
{
 int i;
 printf("Your Balance: nn");
 for(i=0;i<10;i++)
  printf("%d",choc[i]);
  printf("nnnn");
}
void end(void)
{
 system("cls");
 printf("tttTHANK YOU FOR USING THIS SERVICESn");
  printf("tttt  Visit Us At nn");
  printf("ttt    vendingmachine.com.mynnn");
  printf(" First Programmer : Adawiyahn");
  printf(" First Programmer : Vijayah Santhin");
} 

我的程序正确吗?我的程序是否整洁有序?goto语句有那么糟糕吗?对不起,我的英语不好。我正努力在这几天内完成我的项目。我下周要给我的讲师做报告。

对不起,打扰你了。我不知道怎么复制粘贴

在您的第一次尝试中,您的问题是您从他们输入的金额中减去他们选择的数字,而不是他们选择的饮料的成本。您可以非常简单地修改您的程序:

  1. 创建float类型的新变量cost
  2. 在每个case中,在break之前,设置cost为所选饮料的成本。
  3. 而不是:

    change = money - choice;
    

    你会想:

    change = money - cost;
    

另一个你无法解决的问题(如果有更多的钱,显示变化;如果没有的话,说明需要多少额外的费用是相当简单的。首先,你可以看看是否有足够的钱:

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

那么你可以适当地打印:

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

我没有足够的时间来审查你的最新代码,但是你的第一次尝试,虽然它可以有一些进一步的改进,正如其他人在那个线程中建议的,离你需要的地方不远。