I'm making a program that lets user enter a string then program searches various words in the string using strstr function, then calls different functions according to which word is found.I decided to use switch statements to check which words are present.I made a prototype program as:

int main() {
    char str[] = "This is a string.";
    char str1[] = "is";
    int num = strstr(str, str1);

    switch(num) {
        case 0:
            cout<<"Str1 is present";
            break;
        case -1:
            cout<<"str1 is absent";
            break;
    }
}

It's gimme the error:

invalid conversion from 'char*' to 'int' [-fpermissive]

What am I doing wrong?

有帮助吗?

解决方案

strstr returns a char* or const char*

Have a read here.

char str1 = "This is a string.";
char str2 = "is";
char* result = strstr(str1, str2);
if (result == NULL)
{
    cout<<"str1 is absent";
}
// etc
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top