它用于我在C中的编程类,它是一个简单的ATM,可执行4个操作: 提取,存款,Acc详细信息,检查用户余额,并退出。我已经做了简单的基础,但由于项目要求的变化,我现在需要在其中放置密码因素,我已经尝试过并且已经出现了使用以下代码,但我想让它在主菜单之前询问密码,即:存款,取款等...我也尝试过使用开关盒,但我觉得没有改变任何东西。任何帮助非常感谢。
到目前为止,我所做的代码是:
#include<stdio.h>
#include<conio.h>
int atmfun();
main()
{
int option1;
do
option1=atmfun();
while (option1==1);
getch();
}
int atmfun()
{
char Name1[]="Fever";
char Name2[]="JACK";
char Name3[]="JOHN";
char choice='y',choice1='n';
char FullName1[]="Fever tyrell";
char FullName2[]="Jack williams";
char FullName3[]="John Mason";
int i,a,AccBalance = 20000;
int acc_type,password,option,option1;
int passworda=31081;
int passwordb=12345;
int passwordc=12456;
int w_amount,d_amount;
printf("Welcome to generic ATM\n");
printf("\n\nPlease choose an option...");
printf("\n(1)View account details.");
printf("\n(2)Check balance.");
printf("\n(3)Withdraw amount.");
printf("\n(4)Deposit amount.");
printf("\n(5)Exit.\n");
scanf("\n%d",&option);
//start of option 1
if(option==1) //if user wants to view their account details
{
printf("PLEASE ENTER YOUR PASSWORD: ");
scanf("%d",&password);
if
(password!=passworda&&password!=passwordb&&password!=passwordc)
printf("YOU HAVE ENTERED AN INVALID PASSWORD");
else
if(password==passworda)
{
printf("WELCOME Mr.%s\n\n",Name1);
//rest of the account details
}
else
if(password==passwordb)
{
printf("WELCOME Mr.%s\n\n",Name2);
//rest of the acc details
}
else
if(password==passwordc)
{
printf("WELCOME Mr.%s\n\n",Name3);
//rest of the acc details
}
{
printf("\nDo you wish to go back to the main menu?\nEnter 1 for YES, and any other number for NO\n");
scanf("%d",&option1);
if (option1==1)
{
return option1;
}
else
printf("Thank you for using Generic ATM");
return 0;
}
}
// end of option1
// start of option 2
if(option==2) // if user wants to view his account balance
{
printf("Your account balance is $%d",AccBalance);
printf("\nDo you wish to go back to the main menu? \nEnter 1 for YES, and any other number for NO\n");
scanf("%d",&option1);
if (option1==1)
{
return option1;
}
else
printf("Thank you for using Generic ATM");
return 0;
}
//end of option 2
//start of option 3
if(option==3)// if user wants to withdraw money
{
printf("Enter amount to withdraw\n$");
scanf("%d",&w_amount);
{
if(w_amount<=0)
{
printf("Amount cannot be a number under zero or zero");
for(i=4;i>0;i--)
{
printf("\nEnter appropriate amount,you have %d tries left\n",i);
{
printf("Enter amount to withdraw\n$");
scanf("%d",&w_amount);
if(w_amount<=0)
{
printf("You cannot withdraw this amount");
}
}
if (i==1)
printf("\nYou have exceeded your tries\n");
}
}
else
if(w_amount>AccBalance)
{
printf("You don't have sufficient funds, please enter the amount within your Account Balance");
for(i=4;i>0;i--)
{
printf("\nEnter appropriate amount,you have %d tries left\n",i);
{
printf("Enter amount to withdraw\n$");
scanf("%d",&w_amount);
if(w_amount<=0)
{
printf("You cannot withdraw this amount");
}
}
if (i==1)
printf("\nYou have exceeded your tries\n");
}
}
else
{
AccBalance=AccBalance-w_amount;
printf("Your new balance is $%d",AccBalance);
}
}
printf("\nDo you wish to go back to the main menu?\nEnter 1for YES, and any other number for NO\n");
scanf("%d",&option1);
if (option1==1)
{
return option1;
}
else
printf("Thank you for using Generic ATM");
return 0;
}
//end of option 3
//start of option 4
if (option==4) // if user wants to deposit dem moneys bbby
{
printf("Enter amount to deposit\n$");
scanf("%d",&d_amount);
if(d_amount<=0)
{
printf("You cannot deposit this amount");
for(i=4;i>0;i--)
{
printf("\nEnter appropriate amount,you have %d tries left\n",i);
{
printf("Enter amount to deposit\n$");
scanf("%d",&d_amount);
if(d_amount<=0)
{
printf("You cannot deposit this amount");
}
}
if (i==1)
printf("\nYou have exceeded your tries\n");
}
}
else
{
AccBalance=AccBalance+d_amount;
printf("You have deposited $%d",d_amount);
printf("\nYour New balance is $%d",AccBalance);
}
printf("\nDo you wish to go back to the main menu?\nEnter 1 for YES, and any other number for NO\n");
scanf("%d",&option1);
if (option1==1)
{
return option1;
}
else
printf("Thank you for using Generic ATM");
return 0;
}
//end of option 4
//start of option 5
{
if (option == 5);
printf("\nThank you for using Generic ATM\n");
return option;
}
//end of option 5
return 0;
getch();
}
答案 0 :(得分:1)
int main(){
while true{
if (checkPassword()) handleTransaction();
};
};
如果你不在main中做所有事情,看看有多容易?
答案 1 :(得分:0)
要在打印之前询问主菜单之前询问密码(主菜单):
//rest of code
printf("PLEASE ENTER YOUR PASSWORD: ");
do{
scanf("%d",&password);
if(password!=passworda&&password!=passwordb&&password!=passwordc)
printf("YOU HAVE ENTERED AN INVALID PASSWORD");
else break;
}while(true);
if(password==passworda)
{
printf("WELCOME Mr.%s\n\n",Name1);
//rest of the account details
}
else
if(password==passwordb)
{
printf("WELCOME Mr.%s\n\n",Name2);
//rest of the acc details
}
else
if(password==passwordc)
{
printf("WELCOME Mr.%s\n\n",Name3);
//rest of the acc details
}
printf("Welcome to generic ATM\n");
printf("\n\nPlease choose an option...");
printf("\n(1)View account details.");
printf("\n(2)Check balance.");
printf("\n(3)Withdraw amount.");
printf("\n(4)Deposit amount.");
printf("\n(5)Exit.\n");
scanf("\n%d",&option);
//rest of code