自动售货机项目
Vending Machine Project
我需要帮助来编写这个程序,我对C和c++相当陌生。请帮我开始吧。我不知道从哪里开始,但我在尽力。我需要写一个自动售货机程序。
- 在屏幕上显示饮料列表
- 允许用户退出或选择饮料
- 如果用户选择了饮料,他或她将输入要插入机器的金额
#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语句有那么糟糕吗?对不起,我的英语不好。我正努力在这几天内完成我的项目。我下周要给我的讲师做报告。
对不起,打扰你了。我不知道怎么复制粘贴在您的第一次尝试中,您的问题是您从他们输入的金额中减去他们选择的数字,而不是他们选择的饮料的成本。您可以非常简单地修改您的程序:
- 创建
float
类型的新变量cost
- 在每个
case
中,在break
之前,设置cost
为所选饮料的成本。 -
而不是:
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);
}
我没有足够的时间来审查你的最新代码,但是你的第一次尝试,虽然它可以有一些进一步的改进,正如其他人在那个线程中建议的,离你需要的地方不远。
相关文章:
- 如何自动获取我的项目的路径并删除一些文件
- 自动完成在git存储库中的Visual Studio项目中不起作用
- 交叉编译自动制作项目?
- C 自动售货机程序,具有C
- 自动工具不喜欢我的项目目录中的 install.sh 脚本
- 自动售货机程序(帮助)
- 在m64自动工具项目中编译m32库
- 在UWP C++项目中,程序集分配的内存指针自动更改为0xffffffff
- 如何将 Google 测试与通过自动工具构建的项目一起使用
- C++库复制到 C# 项目时自动生成?(Visual Studio 2010)
- 从C++自动执行Microsoft项目
- 自动将目录中的所有文件添加到库项目中
- CMake 项目自动制作库
- C++eclipse项目自动构建服务器
- 如何使用现有代码自动生成/添加/编辑Makefile到Makefile项目
- 结构和阵列自动售货机
- 如何跟踪自动售货机中可用硬币的硬币编号
- 自动售货机项目
- 当环境变量发生变化时,让基于makefile的cmake项目自动运行make-rebuild_cache
- 错误 C2504:"自动售货机":未定义基类