0

This is a part of my 'Phonebook' program.

void viewone(){


    char name[25], fname[25];

    int n, ncheck, op;

    fp = fopen("Phonebook.txt","r");

    printf ("\n Search by : \n 1: Name\n 2: Phone Number\n");
    printf ("Enter option : ");
    scanf ("%d",&op);

    switch(op){

        case 1:
            printf ("\n Enter Name : ");
            scanf ("%s",name);

            fscanf(fp, "%s %d", fname, &ncheck);

            while (!feof(fp)){

                printf ("\n\n %s \n\n",fname);

                if (fname == name){              \\ Problem in here

                    printf ("\n\n Contact Found...\n");

                    printf (" %s +880%d", fname, ncheck);

                    break;

                }

                else{

                    fscanf(fp, "%s %d", fname, &ncheck);

                }

                if (feof(fp)){

                    printf ("\n\n Contact Not Found...\n\n");

                }

            }

            menu();

            break;

        case 2:

            printf ("\n\n Enter Contact Number (+880) : ");
            scanf ("%d",&n);

            fscanf(fp, "%s %d", fname, &ncheck);

            while (!feof(fp)){

                if (ncheck == n){

                    printf ("\n\n Contact Found...\n");

                    printf (" %s +880%d\n", fname, ncheck);

                    break;

                }

                else{

                    fscanf(fp, "%s %d", fname, &ncheck);

                }

                if (feof(fp)){

                    printf ("\n\n Contact Not Found...\n\n");

                }

            }

            menu();

            break;

        default:

            printf ("\n Wrong option...\n\n");

            viewone();

            break;
        }

    }

When it comes to the marked line, the program should search the file for the 'fname' character until it matches the 'name' character. But though they match, nothing happens and the program still goes on. And in the end, it does what is told in the else statement. My question is why is this happening and how can i fix it?

My program runs perfectly when i search with phone number. But why is it not happening with character?

Anik Shahriar
  • 151
  • 1
  • 11

1 Answers1

-1

Comparing string is not done by ==. Instead use string compare function like this

strcmp(fname,name)
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
user5434084
  • 149
  • 1
  • 8