首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动售货机项目

自动售货机项目
EN

Stack Overflow用户
提问于 2013-03-30 05:09:09
回答 1查看 9.4K关注 0票数 0

我需要帮助写这个程序,我是相当新的C和C++。请帮我开始。我不知道从哪里开始,但我已经尽力了。我需要写一个自动售货机程序。

  1. 在屏幕上显示饮料列表
  2. 允许用户退出或选择饮料
  3. 如果用户选择了饮料,他或她将输入要插入机器的钱的数量。

代码语言:javascript
复制
#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");
} 

我的程序正确吗?我的程序整洁有序吗?后藤的说法有那么糟吗?对不起,我的英语不好。我正在尽力在这几天内完成我的项目。下周我要向我的讲师介绍一下。

抱歉打扰你了。我不知道怎么在这里复制和粘贴。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-30 05:24:31

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

  1. 创建一个名为float的新变量cost
  2. 在每个case中,在break之前,将cost设置为他们选择的饮料的成本。
  3. 而不是: 变化=金钱-选择; 你会想: 变更=货币成本;

另一个您无法解决的问题(如果有更多的钱而不是必需的话,请显示更改;如果没有则显示需要额外的多少)是相当简单的。首先,你可以看到是否有足够的钱:

代码语言:javascript
复制
if(money >= cost) {
    // There's enough money.
}else{
    // There's not enough money.
}

然后,您可以适当地打印:

代码语言:javascript
复制
if(money >= cost) {
    printf("Change: %.2f\n", money - cost);
}else{
    printf("You need RM%.2f more.\n", cost - money);
}

我没有足够的时间来检查您的最新代码,但您的第一次尝试,虽然它可以有一些进一步的改进,其他人在该线程中,离您需要它的地方不远。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15715204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档