0

I'm wanting to have my variable houseTot increment by one every time strstr finds a string with "house" in it. I've essentially done this in my code:

struct Owner1_def
{
    int totProps;
    char type[40];
    float monthlyPrice;
    float maintenance;
    int bedrooms;
    int bathrooms;
};
typedef struct Owner1_def Owner1;
int main(void)
{
char *ptr;
int i = 0;
ptr = strstr(database[i].hometype, "house");
for(i = 0; i <= 3; ++i)
{
if (ptr != NULL)
    houseTot++;
}
return 0;
}

But when the program prints houseTot's value, it's still at it's initialized value 0. I don't know much about strstr, but from what I've read, this should work.

Boo92
  • 69
  • 4
  • 1
    You'll need to supply more code than that. https://stackoverflow.com/help/mcve – Michael Albers Mar 26 '17 at 03:44
  • 4
    Shouldn't the call to `strstr()` be inside the loop? Is this actual code? – ad absurdum Mar 26 '17 at 03:47
  • @DavidBowling that fixed my issue; strstr() is totally new to me and all of the examples I looked at didn't use it in any kind of loop, so I was guessing, thanks! – Boo92 Mar 26 '17 at 03:51
  • strstr returns substring if it is available. So your code should work fine. Printf the database to see if it contain house and supply more code – nakul parashar Mar 26 '17 at 03:55

1 Answers1

0

The call to strstr() should be moved inside the loop so that the value stored in database[i].hometype can be checked on each iteration:

for(i = 0; i <= 3; ++i) {
    ptr = strstr(database[i].hometype, "house");
    if (ptr != NULL)
        houseTot++;
}

Note that posted code has no definition for the array of structs database[], and the struct provided has no hometype field.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60