My program is running but not the way it should.
I am having some difficulty with reading a string from the keyboard.
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
//look up if user exist in the sruct array which receive data from a file
struct CellOwner{
char name[30];
char Sname[30];
char msisdn[30];
}owner1;
struct CellOwner AllOwners[40];
int main( )
{
char thename[30];
char theSname[30];
char themsisdn[30];
int col=1;
int a,i=0;
int c;
FILE *fp;
fp = fopen("CellOwners.txt","r");
while ((c=getc(fp))!=EOF)
{
if ((c!=' ')&&(c!='\n')) //c is a character
{
if (col==1)
thename[i++]=c;
else
if (col==2)
theSname[i++]=c;
else
if (col==3)
themsisdn[i++]=c;
}
else
if (c==' ') //einde van col=1 of col=2
{
if (col==1)
{
thename[i]='\0';
strncpy(AllOwners[a].name,thename,i);
++col;
}
else
if (col==2)
{
theSname[i]='\0';
strncpy(AllOwners[a].Sname,theSname,i);
++col;
}
i=0;
}
else
if (c=='\n' && col==3)//end of record
{
themsisdn[i]='\0';
strncpy(AllOwners[a].msisdn,themsisdn,i);
col=1;
i=0;
a++ ;
}
}
themsisdn[i]='\0';
strncpy(AllOwners[a].msisdn,themsisdn,i);
printf("%s","Enter the customer's name\n");
//get name
int customer;
customer = getchar();
while(customer != EOF)
{
putchar(customer);
customer = getchar();
}
printf("%d",customer);
int nm=0;
for(nm=0;nm<20;nm++){
if (AllOwners[nm].name==customer)
printf("%d,%s",customer,"is in the List");
}
printf("\n");
int j=0;
for(j=0;j<20;j++)
printf("%s,%s,%s\n",AllOwners[j].name ,AllOwners[j].Sname,AllOwners[j].msisdn);
return 0;
}
/* gcc testp.c
gcc testp.c -o testp
testp*/
This is what happen when I run it:
-bash-3.00$ testp
Enter the customer's name
JohnJohn-1
John,Smit,2772133143
Piet,Botha,270192389
Carl,vermak,12345890
,,
,,
,,
It should indicate that John is in the list but it aint.Can anyone tell me why?