I'm coding a project for account transaction. However, there are some little problems in my checking account ID process. I used struct function belong string array, which can help me easily to solve my subtasks later, the point is as I try to check the account_ID by using string compare. The output cannot display my expectation. I don't know how to scan a variable in array to check with the string-array struct in File-text.
I endeavoured to scan a variable by so much ways. But it doesn't work. Here is my code.
struct customers{
char phone[13];
char id[9];
char name[31];
char address[201];
char city[31];
char date[11];}customer
int main()
{
FIle *fc
if((fc=fopen("clients.txt","a+"))=NULL)
printf("Can not open");
else
{
int i = 0, a = 0;
fflush(stdin);
customer_ID:
printf("Enter the ID (maximum 8 digits): ";
gets(customer[i].id);
if (strlen(customer[i].id)!=8)
{
printf("Wrong the number of digits ! Enter Again!\n");
goto customer_ID;
}
while(fscanf(fc,"%s\n%[^\n]%*c%s\n%[^\n]%*c%[^\n]%*c%s\n\n",customer[a].id,customer[a].name,customer[a].phone,customer[a].address,customer[a].city,customer[a].date)!=EOF)
{
if (strcmp(customer[i].id,customer[a].id)==0)
{
printf("Account ID has been already used ! Please Enter other ID !\n");
goto customer_ID;
}
}
OUTPUT: * Actual Result: It don't print "Account ID has been already used ! Please Enter other ID !"
* Expected Result:
Print "Account ID has been already used ! Please Enter other ID !"
fflush(stdin);?iandaare both 0. What do you expect is the result of comparing the same string?(fc=fopen("clients.txt","a+"))=NULLwill always be false, and also has a side-effect of settingfcto NULLfscanfmight fail and return values other thanEOF. You are best to compare the return value against the expected number of items to read. Or better still, don't usefscanfsince it can easily overflow your buffers.