#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define I 27
#define IR 25
#define File "test.txt"
struct customer
{
long long identity;
char iban[I];
int money;
};
void filesave(struct customer cus)
{
FILE *file;
file = fopen(File,"a");
if (file==NULL)
{
printf("the file did not open properly.");
}
fprintf(file,"%lld %s %d n",cus.identity,cus.iban,cus.money);
fclose(file);
}
int identitycontrol(long long identity)
{
int a=0,bas;
long long i=1;
while (a<11)
{
bas=(identity/i)%10;
if (bas<0 || bas>9)
{
return 0;
}
i*=10;
a++;
}
if (identity / i > 0 && identity>0) {
return 0;
}
return 1;
}
int ibancontrol(char iban[I])
{
if(!isalpha(iban[0]) || !isalpha(iban[1]))
{
return 0;
}
for (int i = 2; i < I; i++)
{
if (iban[i]<48 || iban[i]>57)
{
return 0;
}
}
return 1;
}
int moneycontrol(int money)
{
if (money>0)
{
return 1;
}
return 0;
}
int transfermoneycontrol(int b, int m)
{
if(b>=m)
return 1;
else
return 0;
}
int moneyorder(char account1[],char account2[],int transfermoney)
{
FILE *file;
file = fopen(File,"r");
struct customer cus[100];
char temp[100];
int y=0,s=0;
while ( fgets(temp,100, file) != NULL )
{
sscanf(temp,"%lld %s %d",&cus[s].identity,cus[s].iban,&cus[s].money);
// printf("%d",strlen(cus[s].iban));
if(strcmp(cus[s].iban,account1)==0)
{
cus[s].money -=transfermoney;
y++;
}
if(strcmp(cus[s].iban,account2)==0)
{
cus[s].money +=transfermoney;
y++;
}
s++;
}
printf("%sn",account1);
printf("%sn",cus[0].iban);
for(int z=0;z<s;z++)
{
printf("nGuncel bilgiler:n");
printf("%d: %s %lld %d n",z+1,cus[z].iban,cus[z].identity,cus[z].money);
}
fclose(file);
file = fopen(File, "w");
if (file == NULL)
{
printf("the file did not open properly.");
return 0;
}
for (int x = 0; x < s; x++)
{
filesave(cus[x]);
}
fclose(file);
return 1;
}
int main()
{
int choice=0,control=0;
struct customer cus;
char account1[I];
char account2[I];
int transfermoney;
while (1)
{
printf("-----Menü-----n");
printf("1:Entryn");
printf("2:Transfern");
printf("3:Exitn");
printf("What is your choice: ");
scanf("%d",&choice);
fflush(stdin);
switch (choice)
{
case 1:
while (1)
{
printf("Please enter your identity: ");
scanf("%lld",&cus.identity);
fflush(stdin);
if (identitycontrol(cus.identity)==1)
{
break;
}
}
while (1)
{
printf("Please enter your iban: ");
scanf("%s",cus.iban);
//fgets(cus.iban,I,stdin);
//cus.iban[strlen(cus.iban)-1]='';
fflush(stdin);
if (ibancontrol(cus.iban)==1)
{
break;
}
}
while (1)
{
printf("Please enter your money: ");
scanf("%d",&cus.money);
fflush(stdin);
if (moneycontrol(cus.money)==1)
{
break;
}
}
filesave(cus);
break;
case 2 :
while(1)
{
printf("Please enter the main account we will send money to: ");
fflush(stdin);
// fgets(account1,I,stdin);
// account1[strlen(account1)-1]='';
scanf("%30s",account1);
fflush(stdin);
// getchar();
if (ibancontrol(account1)==1)
{
break;
}
}
while(1)
{
printf("Please enter the account from which you will receive money: ");
//fgets(account2,I,stdin);
scanf("%30s",account2);
fflush(stdin);
//getchar();
if (ibancontrol(account2)==1)
{
break;
}
}
while(1)
{
printf("Please enter your transfer money: ");
scanf("%d",&transfermoney);
fflush(stdin);
if (moneycontrol(transfermoney)==1)
{
break;
}
}
int a=moneyorder(account1,account2,transfermoney);
if (a==1)
{
printf("Money order sucsessful.n");
}
else if (a==0)
{
printf("Money order unsucsessful.n");
}
break;
case 3:
exit(2);
break;
default:
printf("Wrong choice!!n");
break;
}
}
return 0;
}
After getting the account1 variable, I can print it, but when it enters the world containing account 2, account1 disappears, but I don’t know why.All the remaining functions and file operations are working, but there is a problem in that part.account1 disappears, I cannot press it in a way that I do not understand or the function. When I send it, it’s empty inside, just n
New contributor
Morpen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1