Link to home
Start Free TrialLog in
Avatar of cwchan80
cwchan80

asked on

Need help in comparing string of array in C

This problem should be simple but I just forgot the easier way to handle it. Consider the following program:

=======================================================================================
// dbfile.txt - the text file simulating fairly simple database

101 chan 1800 sales 1998
102 john 4000 sales 2000
103 myke 6000 sales 2001
104 sarah 2800 sales 1994
105 ali 3000 it_dept 1996

=======================================================================================
// source file

#include<stdio.h>
#include<malloc.h>

FILE *dbfile;

struct emp
{
     int id;
     char name[10];
     int salary;
     char dept[10];
     int yearoj;
     struct emp *next;
};

struct emp *temp;
struct emp *list;

void main()
{  
     int j;
     dbfile = fopen("dbfile.txt", "r");
         
     list=NULL;
     for(j=0; j<5; j++)
     {
          temp=(struct emp*) malloc(sizeof(struct emp));
          fscanf(dbfile, "%d", &temp->id);
          fscanf(dbfile, "%s", temp->name);
          fscanf(dbfile, "%d", &temp->salary);
          fscanf(dbfile, "%s", temp->dept);
          fscanf(dbfile, "%d", &temp->yearoj);
          temp->next=NULL;

          if(list==NULL)
               list=temp;
          else
          {
               temp->next=list;
               list=temp;
          }

     }

     temp=list;
     printf("\nThe ID no. of employees who get < 3000 in the sales dept. who join before 1995:\n");
     while(temp!=NULL)
     {
          if(temp->salary < 3000 && temp->dept == "sales" && temp->yearoj < 1995)
               printf("%d\n", temp->id);
          temp=temp->next;
     }

     fclose(dbfile);
}
==========================================================================

There should be an entry found. However, the program can't find any matched entry due to the temp->dept == "sales". I believe I can't match the string like this. May I know in C, how to compare the string?

Thanks and hope to hear from you soon.
Avatar of zebada
zebada

if(temp->salary < 3000 && strcmp(temp->dept,"sales")==0 && temp->yearoj < 1995)
 
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cwchan80

ASKER

Thanks zebada and mayank. The solution works. The ID can be printed correctly.