0

I've edited the question since the solution didn't work as intended for me. Is it possible to write some sort of if statement or any other code for that matter, that prints out an error message when the entered string does not exist in that struct array ? After it prints out an error message, it asks for the string again. I've tried for a while now and can't seem to get it right.

int ordet=0; char_sokafras[20];
printf("Name?\n");
scanf("%s", soka_fras);
while(ordet<*num_items)
{
if(strstr(varor[ordet].name, soka_fras))
{
printf("Name found!\n");
soka[hitta_tecken]=varor[ordet];
hitta_tecken+=1;
}
ordet+=1;
}
2
  • 1
    Did you read the documentation? Commented Oct 7, 2017 at 14:28
  • the question is unclear. For instance, do you want the code to prompt for another string or not? Commented Oct 9, 2017 at 20:29

1 Answer 1

1

strstr returns a pointer to the start of the substring in haystack. From manpages:

If needle is an empty string, haystack is returned; if needle occurs nowhere in haystack, NULL is returned; otherwise a pointer to the first character of the first occurrence of needle is returned.

Amending the while loop to this should be enough:

while (strstr(varor[i].name, soka_fras) != NULL)
Sign up to request clarification or add additional context in comments.

1 Comment

@M.M fixed (need more characters)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.