matching of names in 2d array of strings...

This is a discussion on matching of names in 2d array of strings... within the C Programming forums, part of the General Programming Boards category; Hi , I have this program where in i first add list of names to a 2d array of strings ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    matching of names in 2d array of strings...

    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?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    But the comparison of the userinput is not happening with the stored names is the array.
    So what is happening?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Oh i think when i was inputting the names i was giving a space and then entering the name for instance if i had to enter Rohan i was entering space and then Rohan and i was expecting the program to give correct output. So i thought its doing some weird thing.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The code runs fine. What is the problem?
    Additionally, I would like to point out:
    - http://sourceforge.net/apps/mediawik...arameter_names
    strcpy(&namelist[count][0],s) is the same as strcpy(namelist[count],s), same for the other.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes it runs fine i was inputting the name by giving space and then entering the name and expecting it to behave the correct way. My fault :-(

    I copy pasted the following function
    [insert]
    Code:
    	fputs("enter your name", stdout);
    	fflush(stdout);
    	if( fgets(userip, sizeof userip, stdin) != NULL)
    	{
    		char *newline = strchr(userip, '\n');
    		if(newline != NULL)
    		{
    			*newline = '\0';
    		}
    	}
    I understand the way this is functioning but whats the use of fflush? Is it to tell the compiler to display the text on the console

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    It flushes the output buffer, forcing the text to appear on the screen. Otherwise it might not appear on the screen unless the output buffer is full.
    However, a newline (\n) will also usually flush the output buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    @ Elysia
    Additionally, I would like to point out:
    http://sourceforge.net/apps/mediawik...arameter_names
    Now i had be careful in declaring the prototypes :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2d array of strings
    By localrob in forum C Programming
    Replies: 6
    Last Post: 08-23-2004, 02:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. 2d array problem
    By LiLgirL in forum Windows Programming
    Replies: 1
    Last Post: 03-15-2004, 01:23 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21