Hi ,
I have this program where in i first add list of names to a 2d array of strings and then i ask the user to enter his name. If his name is there he is welcomed else he is told to get out. But the comparison of the userinput is not happening with the stored names is the array.
Everytime if the name matches he is told to get out.
[insert]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX1 2
#define MAX2 10
int add(char *);
int find(char *);
int count = 0;
char namelist[MAX1][MAX2];
int main(void)
{
int flag = 0;
char userip[MAX2];
char *str;
char *name;
flag = add("Akshay");
if(flag == 0)
printf("\nName couldnt be added");
flag = add("Rohan");
if(flag == 0)
printf("\nName couldnt be added");
//printf("\nEnter your name");
//name = gets(str);
//userip = name;
fputs("enter your name", stdout);
fflush(stdout);
if( fgets(userip, sizeof userip, stdin) != NULL)
{
char *newline = strchr(userip, '\n');
if(newline != NULL)
{
*newline = '\0';
}
}
flag = find(userip);
if(flag == 1)
printf("\nWelcome");
else
printf("\nGet out");
return 0;
}
int find(char *s)
{
int i;
int flag = 0;
for(i = 0 ; i< count; i++)
{
if(strcmp(&namelist[i][0], s) == 0)
{
flag = 1;
break;
}
}
return flag;
}
int add(char *s)
{
if(count < MAX1)
{
if(strlen(s) < MAX2)
{
strcpy(&namelist[count][0],s);
++count;
return 1;
}
}
return 0;
}
Also scanf and gets are not good for taking strings as input from the user. Whats the reason behind it?