Checking if it is already exist

I've been working this program for 2 days and I can't figure this out. Unique record means that the program should check if the inputted Account Number already exists. The program should not allow the user to input the same account number. My program always tells that it is "Already Exist! Please try again!" even though the Account Number doesn't exist.


Sample Output:

Enter Account Number: 14344
Enter Account Name: Ichigo Kurasaki
Enter PIN: 9546
Enter Initial Balance: 50000

Create another account [Y/N]: Y

Enter Account Number: 14344
“Account Already Exist! Please try again…"


HERE'S MY PROGRAM:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

struct account{
	char acctnum[5], actname[30];
	char pin[4];
	float intbal;
}bank;


//void item();


int main()
{ 	char again;
	int success = 0;
		char newnum[5], newname[30];
		char npin[4];
		float nbal;
		
	FILE *fp, *fa;
	fp = fopen("Accounts.txt","w");	
	
		
	printf("\n\nEnter account number:\t");
	scanf("\n");
	gets(bank.acctnum);
	printf("Enter account name:\t");
	scanf("\n");
	gets(bank.actname);
	printf("Enter Pin:\t");
	scanf("\n");
	gets(bank.pin);
	printf("Enter Initial Balance:\t");
	scanf("%f", &bank.intbal);
	
	printf("\n\nCreate another account? [Yes/No]:\t");
	scanf(" %c", &again);
	
	fprintf(fp," \n\nEnter account number:\t%.5s \nEnter account name:\t%s \nEnter Pin:\t%s \nEnter Initial Balance:\t%.2f", bank.acctnum, bank.actname, bank.pin, bank.intbal);

	fclose(fp);
	
	do{
		printf("\n\nAccount Number:  ");
		scanf(" \n");
		gets(newnum);
				
		fp = fopen("Accounts.txt","r");
		fa = fopen("tempacct.txt", "w");
							
		while (fscanf(fp,"%s%s%s%f", bank.acctnum, bank.actname, bank.pin, &bank.intbal)!=EOF)	{	
		if(strcmp(newnum,bank.acctnum)==0)//if code is same
			{	
				printf("Enter account name:\t");
				scanf("\n");
				gets(newname);
				printf("Enter Pin:\t");
				scanf("\n");
				gets(npin);
				success = 0;	
				printf("Enter Initial Balance:\t");
				scanf("%f", &nbal);			
				getch();
				success = 1;}
			// end of if newcode the same
						
		else
			{
				fprintf(fa,"%s%s%s%f", bank.acctnum, bank.actname, bank.pin, &bank.intbal);
			}		
			}
					fclose (fp);
					fclose (fa);
	
					remove("Products.txt");
					rename("temproducts.txt","Products.txt");					
					
		if (success==0)
		{	printf("Already Exist!");	}
			
			getch();
			printf("\n\nCreate More? [Y/N]: ");
			scanf(" %c", &again);
			
		}while(toupper(again)=='Y');
			getch();
			success = 0;

	getch();

}
Last edited on
You need to set your success variable to 1 if the account number doesn't exist.
BTW. It's better to use a bool variable and name it like duplicate_number.
my initialization should be like this

int success =1;
I already changed it but it doesn't do the if it exist or not
That's the way you save it.



Enter account number: 1234
Enter account name: Tom
Enter Pin: 1111
Enter Initial Balance: 1000.00

when you read it with fscanf(fp,"%s%s%s%f", bank.acctnum, bank.actname, bank.pin, &bank.intbal
bank.acctnum is "Enteraccount" so strcmp(newnum,bank.acctnum)==0 will always be false;

It would be much easier if you use binary format with fread and fwrite
i'm not very familiar in using binary format so I think I can't do it properly in binary format. what if I will do fprintf(fp," %.5s \n%s \n%s \n%.2f", bank.acctnum, bank.actname, bank.pin, bank.intbal); will it not be always false for strcmp(newnum,bank.acctnum)==0
Last edited on
I very confused sorry for asking too much
Actually binary format is easier to use.

FILE fp = fopen("Accounts.bin","wb");

fwrite(&bank, sizeof(bank), 1, fp);

while(fread(&bank, sizeof(bank), 1, fp))
{
// do sth. bank
}
Topic archived. No new replies allowed.