我需要帮助来编写这个程序我是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 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");
}
我的程序是否正确?我的程序整洁有序吗? goto声明是不是很糟糕?对不起,我的英语不好。我正在努力在这几天内完成我的项目。我需要在下周向我的讲师介绍。
很抱歉打扰你。我不知道如何在这里复制和粘贴。
答案 0 :(得分:0)
在您的第一次尝试中,您的问题是您从他们输入的金额中减去他们选择的数字,而不是他们选择的饮料的费用 。你可以简单地修改你的程序:
float
的{{1}}类型的新变量。cost
中,在case
之前,将break
设置为他们所选饮料的费用。而不是:
cost
你想要:
change = money - choice;
你无法解决的另一个问题(如果有更多的钱而不是必要的话显示变化;如果没有,则显示需要多少额外的东西)相当简单。首先,你可以看看是否有足够的钱:
change = money - cost;
然后你可以适当打印:
if(money >= cost) {
// There's enough money.
}else{
// There's not enough money.
}
我没有足够的时间来审核您的最新代码,但是您的第一次尝试,虽然它可以根据该线程中的其他人的建议进行一些进一步的改进,但距离您需要它的地方并不远。