0

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 !"
7
  • 1
    Who or what text suggested using fflush(stdin);? Commented Jun 6, 2019 at 2:04
  • 1
    Code does not compile, consider re-posting with true compilable code. Commented Jun 6, 2019 at 2:08
  • i and a are both 0. What do you expect is the result of comparing the same string? Commented Jun 6, 2019 at 2:08
  • 1
    Side-note: (fc=fopen("clients.txt","a+"))=NULL will always be false, and also has a side-effect of setting fc to NULL Commented Jun 6, 2019 at 2:08
  • Also note that fscanf might fail and return values other than EOF. You are best to compare the return value against the expected number of items to read. Or better still, don't use fscanf since it can easily overflow your buffers. Commented Jun 6, 2019 at 2:12

1 Answer 1

1

maybe try use strstr in case that the id is always in the same size so that it will be

if (strstr(customer[i].id,customer[a].id))

and that way your Durable code spaces

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.