Thread: string compare problem

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    10

    string compare problem

    Hello i have a problem with getting the input from a user to match with values in an array of records..

    here is my code

    Code:
    #include <stdio.h>#include <conio.h>
    
    
       struct records
    		{
    			char name[10];
    			};
    
    
    		struct records n[2] =
          {
          	{"andre"},
             {"brown"}
             };
    
    
       int main()
       {
       	//want to scanf a value to test if the name exist;
    
    
          char input[10];
          int i;
    
    
          printf ("Enter name: ");
          scanf ("\n\n%s", input);
    
    
          for (i = 0; i < 2; i++)
          {
          	if (n[i].name == input)
          	{
          		printf ("Name Exist in record %d \n", i);
             	}
             	else
             	{
             			printf ("Name does not exist in record %d \n", i);
                	}
            }
          getch();
          }
    using a watch i found out tht.. the input from the user if the user types "andre" is being stored as "andre\0@\0x01\0"

    how do i get my program to jst store the word the user types?

    please help

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by leocfox View Post
    how do i get my program to jst store the word the user types?
    It does, the remaining characters of the array are uninitialized, there containing random values. Try doing something like:
    Code:
    char input[10] = { 0 };
    this should initialize all characters to zero.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    I got the same result.. name does not exist in both records.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    now its trying to compare "andre" with "andre\0\0\0\0\0"

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    n[i].name == input
    What do you think that code does? HINT: Both are pointers

    EDIT: You need "strcmp(n[i].name, input) == 0" instead.
    Last edited by GReaper; 04-30-2012 at 01:26 PM.
    Devoted my life to programming...

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by leocfox View Post
    how do i get my program to jst store the word the user types?
    You don't need to. For all intents and purposes, it does.
    The problem is that you need to fix line 32, because you can't compare strings using just == in C.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    10
    thank you..
    it works when i replace "if(n[i].name == input)" with "if (strcmp(n[i].name, input) == 0)"

    one more question "#include <string.h> that made strcmp() work is C programming right?
    My teach dont want us to mix up C with C++

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    string.h is definitely standard C.

    That's very good that your teacher is making sure you don't produce a mix of C and C++.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string compare
    By ingeniousreader in forum C Programming
    Replies: 7
    Last Post: 03-02-2012, 12:43 PM
  2. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  3. String Compare
    By almo89 in forum C Programming
    Replies: 1
    Last Post: 11-04-2005, 08:13 PM
  4. string compare problem
    By gandalf_bar in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2004, 11:11 AM
  5. need help on string compare
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-07-2002, 08:55 PM