strstr function help?

Hello! I am making a program for which one has to made a 2D array, containing the names of jockeys, and one has to make a function which will search through the array and name which column the jockey was found in.

The trouble I'm having is that the loop always seems to end prematurely, giving the result that whatever you inputted is found, whether or not it is actually in one of the names.

When compiled, it doesn't crash or anything, the program just stops doing anything after you input the search criteria. It doesn't end, it just seems to pause.

C++, here is the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void nameInsert(char [][30]);

void search(char [][30]);

main()
{

char name[10][30]; //initalizes variable

nameInsert(name);
search(name);

system("PAUSE");
return 0;

}



//PROGRAM USED TO INPUT NAMES INTO ARRAY
void nameInsert(char str[][30])
{

printf("There are ten jockeys! Insert their names! \n");

for(int z=0; z<10; z++)
     {
       printf("\nInsert name of jockey #%d: ", z+1);
       scanf("%s", str[z]);
     }                
}




//PROGRAM USED TO SEARCH THROUGH ARRAY
void search(char str[][30])
{
 
char find[30];
int x=1; //indicator variable. 0 means 'true' and 1 means 'false'
int count=0;
 
printf("\n\nWhich jockey do you want to search for?\n");  //allows user to input name to search for
scanf("%s", find);
fflush(stdin);

for(count=0; x!=0 || count<15; count++) //for loop that uses strstr to search through array
    {
       if(strstr(str[x], find) != NULL)
          {
           x=0;
          }
           else
              { x=1;}
    }

if(count>10) //publishes different outputs depending on count.
    {
     printf("There were no names containing \"%s\".", find);
    }
       else
         {
         printf("\n\nThe word was found in column %d.\n\n", count); 
         printf("The jockey's position in the table was column %d.\n\n", count); 
         }

}
I changed your code a little bit though I would have implemented this whole program way differently.

Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void nameInsert();

void search();

char name[10][30]; //initalizes variable
char find[30];

main()
{



nameInsert();
search();

system("PAUSE");
return 0;

}



//PROGRAM USED TO INPUT NAMES INTO ARRAY
void nameInsert()
{

printf("There are ten jockeys! Insert their names! \n");

for(int z=0; z<10; z++)
     {
       printf("\nInsert name of jockey #%d: ", z+1);
       scanf("%s", name[z]);
     }
}




//PROGRAM USED TO SEARCH THROUGH ARRAY
void search()
{

int x=1; //indicator variable. 0 means 'true' and 1 means 'false'
int count=0;

printf("\n\nWhich jockey do you want to search for?\n");  //allows user to input name to search for
scanf("%s", find);
fflush(stdin);

for(count=0; count<10; count++) //for loop that uses strstr to search through array
    {
       if(strstr(name[count], find) != NULL)
          {
           break;
          }
        else
            continue;
    }

if(count>9) //publishes different outputs depending on count.
    {
     printf("There were no names containing \"%s\".", find);
    }
       else
         {
         printf("\n\nThe word was found in column %d.\n\n", count);
         printf("The jockey's position in the table was column %d.\n\n", count);
         }

}
Topic archived. No new replies allowed.