gpt4 book ai didi

c - 如何在算术运算中使用文件中的结构变量?

转载 作者:行者123 更新时间:2023-11-30 19:27:46 27 4
gpt4 key购买 nike

我有这个项目,一个使用 c 语言文件和结构的 ATM 帐户系统。现在,除了存款/取款功能之外,我几乎所有功能都已关闭。如果我没记错的话,您只需读取文件并调用要在算术运算中使用的变量,然后将其写回文件以更新它。

如果用户为所述变量输入一个新值,它就可以正常工作(但我不能这样做,因为用户可以随意更改他/她的帐户余额)。

但是当我这样做时,它不起作用。

简而言之,我的问题是我的交易(用户输入存款金额,系统将该金额添加到保存在二进制文件中的用户帐户余额中,然后系统获取新的这两个金额的总和)帐户余额然后覆盖旧帐户余额)具体不起作用,我的结构和用户输入变量没有相加

这是我的功能

void deposit()
{
system("cls");
FILE *fp,*fp1;
struct acc rec,rec1;
int id,pinc,found=0,count=0;
float dv=0,wv=0;
fp=fopen(fname,"rb");
fp1=fopen("tempacc.dat","wb");

printf("\nEnter Account No :");
scanf("%d",&id);
printf("\nEnter PIN :");
scanf("%d",&pinc);

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
if(rec.no==id && rec.pin==pinc)
{
found=1;
printf("current balance:%0.2f",&rec.id);
printf("\nEnter initial deposit:");
scanf("%d",&rec.id);
printf("\nEnter Deposit value:");
scanf("%f",dv);
rec.id = rec.id + dv; <--- (this part doesn't work)
printf("\nNew Account Balance:%f",rec.id);
fwrite(&rec,sizeof(rec),1,fp1);
}
else
{
fwrite(&rec,sizeof(rec),1,fp1);
}
}
fclose(fp);
fclose(fp1);

if(found==0)
{
printf("Sorry No Record Found\n\n");
}
else
{
fp=fopen(fname,"wb");
fp1=fopen("tempacc.dat","rb");

while(1)
{
fread(&rec,sizeof(rec),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&rec,sizeof(rec),1,fp);
}

}
fclose(fp);
fclose(fp1);
}

我的主要内容

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct acc
{
int no;
char name[100];
int pin;
float id;
};

void append();
void display();
void displayAll();
void modify();
void search();
void deposit();

char mygetch();

char fname[]={"accdb.dat"};

int main()
{
char ch;

while(1)
{
system("cls"); //clrscr();

printf("==================Welcome to Banking System=============\n\n");

printf("A. Open an Account \n\n");
printf("B. Bank transaction \n\n");
printf("C. Exit\n\n");
//printf("D. Display\n\n");
//printf("E. Display All\n\n");
//printf("F. Search\n\n");

printf("========================================================\n\n");

printf("\nPlease enter your Choice:");
scanf("%c",&ch);

switch(ch)
{
case 'A': append();
break;

case 'B': modify();
break;

case 'C': exit(0);

case 'D': display();
break;

case 'E': displayAll();
break;

case 'F': search();
break;

}

mygetch();
}

return 0;
}
void append()
{
system("cls");
FILE *fp;
struct acc rec;

fp=fopen(fname,"ab");

printf("\nEnter Account no:");
scanf("%d",&rec.no);
getchar();
printf("\nEnter Account name:");
scanf("%[^\n]s",rec.name);
printf("\nEnter PIN:");
scanf("%d",&rec.pin);
printf("\nEnter Initial Deposit:");
scanf("%f",&rec.id);
fwrite(&rec,sizeof(rec),1,fp);

fclose(fp);
}

void display()
{
system("cls");
FILE *fp;
struct acc rec;
int id,found=0;

fp=fopen(fname,"rb");

printf("\nEnter the account number:");
scanf("%d",&id);

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
if(rec.no==id)
{
found=1;
printf("\n========================================================\n\n");
printf("\t\t Account Details of %d\n\n",rec.no);
printf("========================================================\n\n");

printf("Name\tBalance\n\n");

printf("%s\t",rec.name);
printf("%0.2f\t\n\n",rec.id);

printf("========================================================\n\n");
}
}
if(found==0)
{
printf("\nSorry No Record Found");
}
fclose(fp);

}

void deposit()
{
system("cls");
FILE *fp,*fp1;
struct acc rec,rec1;
int id,pinc,found=0,count=0;
float dv=0,wv=0;
fp=fopen(fname,"rb");
fp1=fopen("tempacc.dat","wb");

printf("\nEnter Account No :");
scanf("%d",&id);
printf("\nEnter PIN :");
scanf("%d",&pinc);

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
if(rec.no==id && rec.pin==pinc)
{
found=1;
printf("current balance:%0.2f",&rec.id);
printf("\nEnter initial deposit:");
scanf("%d",&rec.id);
printf("\nEnter Deposit value:");
scanf("%f",dv);
rec.id = rec.id + dv;
//scanf("%f",&rec.id);
printf("\nNew Account Balance:%f",rec.id);
//scanf("%f",&rec.id);
fwrite(&rec,sizeof(rec),1,fp1);
//fflush(stdin);


}
else
{
fwrite(&rec,sizeof(rec),1,fp1);
}
}
fclose(fp);
fclose(fp1);

if(found==0)
{
printf("Sorry No Record Found\n\n");
}
else
{
fp=fopen(fname,"wb");
fp1=fopen("tempacc.dat","rb");

while(1)
{
fread(&rec,sizeof(rec),1,fp1);

if(feof(fp1))
{
break;
}
fwrite(&rec,sizeof(rec),1,fp);
}

}
fclose(fp);
fclose(fp1);
}

void search()
{
FILE *fp;
struct acc rec;
int found=0;
char name[20];

fp=fopen(fname,"rb");

printf("\nEnter the Account Name:");
scanf("%s",&name);

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
if(strcmp(name,rec.name)==0)
{
printf("\n========================================================\n\n");
printf("\t\t Account Details of %d\n\n",rec.no);
printf("========================================================\n\n");

printf("Name\tbalance\n\n");

printf("%s\t",rec.name);
printf("%0.2f\t\n\n",rec.id);

printf("========================================================\n\n");

}
}
if(found==0)
{
printf("\nSorry No Record Found");
}
fclose(fp);
}

void displayAll()
{
system("cls");
FILE *fp;
struct acc rec;

fp=fopen(fname,"rb");

printf("\n========================================================\n\n");
printf("\t\t All Account Details\n\n");
printf("========================================================\n\n");

printf("ID\tName\tPIN\tBalance\n\n");

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
printf("%d\t",rec.no);
printf("%s\t",rec.name);
printf("%d\t",rec.pin);
printf("%0.2f\t\n\n",rec.id);


}
printf("========================================================\n\n");

fclose(fp);
}

char mygetch()
{
char val;
char rel;

scanf("%c",&val);
scanf("%c",&rel);
return (val);
}

void modify()
{
system("cls");
FILE *fp,*fp1;
struct acc rec,rec1;
int id,pinc,found=0,count=0;

fp=fopen(fname,"rb");
fp1=fopen("tempacc.dat","wb");

printf("\nEnter Account No:");
scanf("%d",&id);
printf("\nEnter PIN:");
scanf("%d",&pinc);

while(1)
{
fread(&rec,sizeof(rec),1,fp);

if(feof(fp))
{
break;
}
if(rec.no==id && rec.pin==pinc)
{
found=1;
int sch;

printf("\tWelcome to Banking System\n");
printf("1.Balance Inquiry\n");
printf("2.Deposit\n");
printf("3.Withdrawal\n");
printf("4.quit\n");
scanf("%d",&sch);

switch (sch)
{
case 1:
display();
break;
case 2:
fclose(fp);
deposit();
break;
case 3:
//withdrawal();
break;
case 4:
printf("Thank you for your patronage\n");
exit(0);
break;
}
}
}
}

最佳答案

由于缺乏更多详细信息,我猜测您的 scanf 组合实际上是在读取 %d%f,即立即读取十进制数后面跟着一个 float 。如果是这样的话,它应该可以工作,在某种程度上:如果您输入123.456,它应该接受初始存款123和新存款 0.456.

但我猜你的意思是让用户输入两个不同的数字,并在它们之间输入。如果是这样,您的输入将是第一个数字,然后是换行符,然后是第二个数字。您需要将其解析为 %d %f (因为空格可以匹配任意数量的空白,包括换行符)。如果您不希望第一个 scanf 等到第一个数字后的第一个非空格,我建议您在第二个 scanf 中包含空格,即进行扫描“%f”

如果所有这些都是错误的,很可能您的 scanf 仍然有问题,因此您应该对此进行错误检查。阅读手册,看​​看有效输入应该返回什么,然后让你的代码提示,如果这不是它得到的。

关于c - 如何在算术运算中使用文件中的结构变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55418704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com