if-else statement error

#include <stdio.h>

main()
{
char str [80];

printf ("Do you want to insert lines to your program?\n");
scanf ("%s",str);
printf(str);
if (str=="yes")
{
printf ("yessss!!!!\n");
}

else if (str=="no")
{
printf("ok then~");
}

else
printf("lalla\n");
return 0;
}

---------------------------------

according to the code above, the program do not enter the if else statement. how can i make it str=="yes" or str=="no" valid?

i am not sure when to use the sign ' or ".

thank you.
You need to use strcmp or strncmp to compare 'strings' in C.
#include <iostream.h>

int main()
{

char str ;
cout>> ("Do you want to insert lines to your program?\n");
cin>> str;
if (str=="yes")

{

cout<<printf ("yessss!!!!\n"<<endl);
}

if (str=="no")
{
cout<< "ok then"<<endl;
}
cout<<"~"<<endl;
}

else
cout<<"lalla"<<endl;
return 0;
}
hi try this.......#include <iostream.h>

int main()
{

char str ;
cout>> ("Do you want to insert lines to your program?\n");
cin>> str;
if (str=="yes")

{

cout<<printf ("yessss!!!!\n"<<endl);
}

if (str=="no")
{
cout<< "ok then"<<endl;
}
cout<<"~"<<endl;
}

else
cout<<"lalla"<<endl;
return 0;
}
[EDIT]
Hey! Wait a second!

You aren't the OP!

If you are going to post code to help someone, make sure it compiles and runs first.

Here is to help the OP:
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
#include <stdio.h>
#include <string.h>

main()
{
    char str [80];

    printf ("Do you want to insert lines to your program?\n");
    fgets( str, 80, stdin );
    printf( "%s\n", str );
    if (strcmp( str, "yes" ) == 0)
    {
        printf ("yessss!!!!\n");
    }

    else if (strcmp( str, "no" ) == 0)
    {
        printf("ok then~");
    }

    else
        printf("lalla\n");

    return 0;
}

[/EDIT]

You've switched to C++. You are very close. I'll make a few small changes for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    cout << "Do you want to insert lines into your program?\n";
    getline( cin, str );
    if (str == "yes")
    {
        cout << "yessss!!!!\n";
    }
    else if (str == "no")
    {
        cout << "ok then\n";
    }
    else
    {
        cout << "lalla\n";
    }
    return 0;
}


Hope this helps.
Last edited on
Topic archived. No new replies allowed.