【发布时间】:2018-01-24 21:11:57
【问题描述】:
我正在做一个名为 ATM 的迷你项目,作为冬季任务。我已经完成了 ATM 的编码,如下面的代码所示。但是我遇到了一个问题,假设我已经为用户创建了一个数据库并将其存储到文件中,但是对于第一个用户,即第一个条目,我可以轻松读取数据,但是对于文件中的第二个条目,我无法读取第一次尝试。在第一次尝试它显示错误Id 和PIN 但在第二次进入时一切正常。列表中的第三个条目(即第三个用户)也发生了同样的事情。对于前两次尝试,它显示错误 ID 和 PIN 但在第三次尝试中一切正常。以此类推所有用户,即第四、第五等。
这是我的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct user
{
int id;
char fname[20],lname[20];
long unsigned int acct_num, mobile;
int pin;
float balance;
};
void balance_enquiry();
void balance_withdraw();
void balance_deposit();
void account_creation();
void account_modify();
void account_auth_id();
void account_auth_pin();
char data[] = {"ATM.dat"};
int main()
{
int option;
G:printf("\nWelcome To The ATM Services:\n");
printf("\n****************************\n");
printf("\nPlease Choose Your Options:\n");
printf("\n");
printf("\n1. Banking Services:\n");
printf("\n2. Account Creation:\n");
printf("\n3. Account Modification:\n");
scanf("%d", &option);
switch (option)
{
case 1:
{
system("clear");
int choice;
account_auth_id();
account_auth_pin();
printf("\n********Welcome to ATM Service**************\n");
printf("\n1. Check Balance\n");
printf("\n2. Withdraw Cash\n");
printf("\n3. Deposit Cash\n");
printf("\n4. Quit\n");
printf("\n******************?**************************?*\n\n");
printf("\nEnter Your Choice: \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
balance_enquiry();
break;
case 2:
balance_withdraw();
break;
case 3:
balance_deposit();
case 4:
printf("\nTHANK U FOR USING ATM \n");
break;
default:
printf("\nINVALID CHOICE:\n");
break;
}
break;
}
case 2:
account_creation();
break;
case 3:
account_modify();
break;
case 4:
printf("\nTHANK U FOR USING ATM \n");
break;
default:
printf("\nEnter Valid Choice:\n");
break;
}
char transaction;
printf("\nDO U WANT TO PERFORM ANOTHER TRANSCATION (y or n): \n");
scanf(" %c", &transaction);
if (transaction == 'y')
goto G;
printf("\nTHANKS FOR USING OUT ATM SERVICE:\n\n");
getch();
return 0;
}
void balance_enquiry()
{
FILE *fp;
struct user t;
int id, f = 0;
fp = fopen(data, "rb");
printf("\nEnter Your ID Number:\n");
D:scanf("%d", &id);
while (1)
{
fread(&t, sizeof(t), 1, fp);
if (feof(fp))
break;
if (t.id == id)
{
f = 1;
printf("Your Current Balance Is: %f\n", t.balance);
}
if (f == 0)
{
printf("Please Enter valid ID:\n");
goto D;
}
}
fclose(fp);
}
void balance_withdraw()
{
FILE *fp,*fp1;
struct user t1;
int withdraw,id, f = 0;
fp = fopen("ATM.dat", "rb");
fp1 = fopen("temp.dat", "wb");
printf("\nEnter Your ID Number:\n");
scanf("%d", &id);
while (1)
{
fread(&t1, sizeof(struct user), 1, fp);
if (feof(fp))
break;
if (t1.id == id)
{
f = 1;
printf("\nENTER THE AMOUNT TO WITHDRAW: \n");
A:scanf("%d", &withdraw);
if (withdraw % 100 != 0)
{
printf("\nPLEASE ENTER THE AMOUNT IN MULTIPLES OF 100:\n");
goto A;
}
if (withdraw > t1.balance)
{
printf("\nINSUFFICENT BALANCE:\n");
printf("\nTRY WITH LESSER AMOUNT\n");
goto A;
if (withdraw <= t1.balance)
{
t1.balance = t1.balance - withdraw;
printf("\nPLEASE COLLECT CASH:\n\n");
printf("\nYOUR CURRENT BALANCE IS: %d", t1.balance);
fwrite(&t1, sizeof(struct user), 1, fp1);
}
else
fwrite(&t1, sizeof(struct user), 1, fp1);
}
}
fclose(fp);
fclose(fp1);
if (f == 0)
{
printf("Sorry, There Is Some Technical Problem:\n\n");
}
else
{
fp = fopen("ATM.dat", "wb");
fp1 = fopen("temp.dat", "rb");
while (1)
{
fread(&t1, sizeof(struct user), 1, fp1);
if (feof(fp1))
break;
fwrite(&t1, sizeof(struct user), 1, fp);
}
}
fclose(fp);
fclose(fp1);
}
}
void balance_deposit()
{
}
void account_auth_id()
{
FILE *fp;
struct user t;
int id, f = 0,k =0;
fp = fopen(data, "rb");
fflush(stdin);
A:printf("\nEnter Your ID Number:\n");
scanf("%d", &id);
while (1)
{
fread(&t, sizeof(t), 1, fp);
if (feof(fp))
break;
if (t.id == id)
{
f = 1;
printf("Welcome, Mr %s\n", t.fname);
}
if (f == 0)
{
k++;
if(k == 3)
{
printf("You Have Reached Max. Attempts:\n");
getch();
return 0;
}
printf("You Have Entered Wrong ID:\n");
printf("Please Enter Valid ID:\n");
goto A;
}
}
}
void account_auth_pin()
{
FILE *fp;
struct user t;
int tmpPin,k = 0, f = 0;
fp = fopen(data, "rb");
fflush(stdin);
B:printf("Enter Your Secret Pin:\n");
scanf("%d", &tmpPin);
while (1)
{
fread(&t, sizeof(t), 1, fp);
if (feof(fp))
break;
if (t.pin == tmpPin)
{
f = 1;
printf("Super!,Now You Can Enjoy Banking Services:\n");
}
if (f == 0)
{
k++;
if(k == 3)
{
printf("You Have Entered Three Times Wrong PIN:\n");
printf("Please,Try Again:\n");
getch();
return 0;
}
printf("You Have Entered Wrong PIN:\n");
printf("Please Enter Correct PIN:\n");
goto B;
}
}
}
void account_creation()
{
FILE *fp;
struct user t;
fp = fopen(data, "ab");
printf("Enter Your ID Number:\n");
scanf("%d", &t.id);
printf("\nEnter Your First Name:\n");
scanf("%s", &t.fname);
printf("\nEnter Your Last Name:\n");
scanf("%s", &t.lname);
printf("Enter Your New Account number:\n");
scanf("%lu", &t.acct_num);
printf("Enter Your Mobile Number:\n");
scanf("%lu", &t.mobile);
printf("Enter Your Secret PIN:\n");
scanf("%d", &t.pin);
printf("Enter The initial Amount To Deposit:\n");
scanf("%f", &t.balance);
fwrite(&t, sizeof(t), 1, fp);
fclose(fp);
}
void account_modify()
{
}
【问题讨论】:
-
这是什么:
G:printf("\nWelcome To The ATM Services:\n");? -
如果用户想要进行另一笔交易,我在代码中使用了 goto G。
-
还有
D:scanf...还有更多,它们是什么? -
@AlexQuilliam 这是一个标签,一个可怕的
goto G;的目标 -
@RajeshDubey 使用
goto是一种不好的做法,并且只能在极少数情况下使用(例如,跳出双 for 循环)。 xkcd.
标签: c structure file-handling fwrite fread