1

I've got a function that searches through a list of names and I'm trying to get the search function to return the index of the array back to the main function and print out the starting location of the name found. Everything I've tried up to this point either crashes the program or results in strange output.

Here is my search function:

#include<stdio.h>
#include<conio.h>
#include<string.h>

#define MAX_NAMELENGTH 10
#define MAX_NAMES 5

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys, int i);
int search(char names[MAX_NAMES][MAX_NAMELENGTH], int Number_entrys);

int main()
{
   char names[MAX_NAMES][MAX_NAMELENGTH];
   int i, Number_entrys,search_result,x;
   printf("How many names would you like to enter to the list?\n");
   scanf("%d",&Number_entrys);
   initialize(names,Number_entrys,i);
   search_result= search(names,Number_entrys);
   if (search_result==-1){
      printf("Found no names.\n");
   }else
   {
      printf("%s",search_result);
   }
   getch();
   return 0;
}

void initialize(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys,int i)
{
   if(Number_entrys>MAX_NAMES){
      printf("Please choose a smaller entry\n");
   }else{
      for (i=0; i<Number_entrys;i++){
         scanf("%s",names[i]);
      }
   }
}

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
}

Like I mentioned before I have tried a lot of different ways to try and fix this issue, but I cant seem to get them to work. Printing X like what I have above is just the last thing I tried, and therefor know that it doesn't work. Any suggestions on the simplest way to do this?

  • `strstr()` returns valid address upon success! not `0`. – जलजनक Oct 30 '12 at 21:05
  • 2
    `char names[MAX_NAMES][MAX_NAMELENGTH];` should be `char names[MAX_NAMELENGTH][MAX_NAMES];` Also, you want `strcmp()` instead of `strstr()`. And a good C book. And formatting your code. –  Oct 30 '12 at 21:05
  • Why do you print out x? Shouldn't it be search_result instead? Also, the "%s" spec is for strings, not numbers. – Fyodor Soikin Oct 30 '12 at 21:06
  • @sparkot That was a typo, I had originally had `strcmp` instead of `strstr` thats why it was set to `==0`. The original post has been updated –  Oct 30 '12 at 21:09
  • @fyodorsoikin I've tried printing out search_result instead, and the program crashes. As for X, like I said originally printing X was just the last thing I tried before I came here, I know it doesn't work ;). –  Oct 30 '12 at 21:11
  • @user1781966 Well, trying to print an integer with the `%s` format specifier will often crash a program, as on many platforms, not every possible integer value is a valid pointer, and even if it is, it most likely isn't currently pointing at a valid null-terminated string. – twalberg Oct 30 '12 at 21:14
  • I lost interest brrrrr. However, share `initialize()` code as well. – जलजनक Oct 30 '12 at 21:20
  • @Sparkot what if the winning answer got you a life time supply of free air? eh, eh, if that doesn't peek your interest then I don't know what will. –  Oct 30 '12 at 21:23
  • code has been updated with the initialize function –  Oct 30 '12 at 21:32

1 Answers1

0

Why don't you use strcmp instead of strstr ?

In your code it seems that there is some huge issues : -it seems that i is use not initialize. -You declare x as a int and then use : printf("%s",x) it's kind of a non-sense here. And by the way not initiliaze !

Something like that should be better (Do note that I haven't your initialize function) and I haven't try to compile :

int search(char names[MAX_NAMES][MAX_NAMELENGTH],int Number_entrys)
{
   int x =0;
   char new_name[MAX_NAMELENGTH];
   printf("Now enter a name in which you would like to search the list for\n");
   scanf("%s",new_name);

   for(x = 0; x < Number_entrys; x++) 
   {
      if ( strcmp( new_name, names[x] ) == 0 )
      {
         return x;
      }
   } 
   return -1;        
 }

MAIN :

int main()
{
    char names[MAX_NAMES][MAX_NAMELENGTH];
    int i=0;
    int Number_entrys=0;
    int search_result=0;
    printf("How many names would you like to enter to the list?\n");
    scanf("%d",&Number_entrys);
    initialize(names,Number_entrys,i); // I guess it is use to initialize names ?!?
    search_result= search(names,Number_entrys);
    if (search_result==-1)
    {
         printf("Found no names.\n");
    }
    else
    {
       printf("Index found in position %d in the tab\n",search_result);
    }
   getch(); //not really a fan of this...
   return 0;
 }

Hope it helps.

Regards,

Joze

Joze
  • 1,285
  • 4
  • 19
  • 33
  • I updated the originally post to include the entire code. As of right now the positioning works only on the first name entered. for example if I have 3 names i get a 1 back for all 3 names. –  Oct 30 '12 at 21:35
  • Do you mean that you want to be able to find more than one position ? – Joze Oct 30 '12 at 21:52
  • I actually am not having the problem I thought I was having. I forgot arrays start with an index of 0, rather then 1, so everytime I printed out a list of 2 names and searched through them, I was typing in the second one expecting to get 2, instead I was getting 1. I realize there is a way to fix this, and I'm currently trying to remember how to do that now lol. :P –  Oct 30 '12 at 22:13