vendredi 26 août 2016

C++ If, else if. Program only reading if statement and neglecting else if [duplicate]

This question already has an answer here:

I can't quite figure this out. The program builds no problem. The issue is that no matter what the user input is, the if statement goes ahead and the program does not check the else if statement. I assume there's a larger structural problem in my script, but I'm not sure what it is.

It could be a problem with the find function. I'm still new to this function.

It could also be to do with how I'm nesting this new if-else-if statement within another if statement.

Here's my code. The problem comes towards the end of the code, in the final if-else-if. I'm pasting the whole thing in case it's a deeper issue elsewhere in the code. The previous if statement works fine.

Thanks for any help. Much appreciated :)

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int emotion;


void Sleep()
{
    Sleep();
}

void loneliness()
{
    cout << "Lonely. You must be strong, and loved, if you choose this feeling to explore.";
}

void inlove()
{
    cout << "In love. You've been watching telenovelas again.";
}

void ambitious()
{
    cout << "Ambitious. Steve Jobs Steve Jobs.";
}

void happy()
{
    cout << "Happy. Is this a thing you can just feel?";
}

int main() {

    string input;
    string input2;
    string name;

cout << "I'm bad." << endl;
cout << "My name's Raab." << endl;
cout << "Your name?" << endl;
getline(cin, name);
cout << "Tell me in one word what emotion you're feeling right now." << endl;
getline(cin, input);
cout << "Haha. " << input << "?" << " " << "Alright. I can work with that." << endl;

cout << "..." << endl;
cout << "Okay, I'm going to give you a choice." << endl;
cout << "You can trade that emotion for one other, if you'd like." << endl;
cout << "Would you like to do that?" << endl;
getline(cin, input2);

if (input2 == "Yes" && "yes") {

    int emotion;

    cout << "Nice one, bro.\n";
    Sleep(350);
    cout << "Here are your options!\n";
    Sleep(300);
    cout << "1. Lonely\n";
    cout << "2. In love.\n";
    cout << "3. Ambitious.\n";
    cout << "4. Happy.\n";
    cin >> emotion;

switch (emotion) {
case 1:
    loneliness();
    break;
case 2:
    inlove();
    break;
case 3:
    ambitious();
    break;
case 4:
    happy();
    break;
}
cin.get();


}

else if (input2 == "No" && "no")
{

    std::string song;
    std::string str2 ("like");
    std::string str3 ("off");

    cout << "Well" << endl;
    Sleep(250);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("whip"), NULL, SND_ASYNC|SND_FILENAME);
    cout << "." << endl;
    Sleep(300);
    PlaySound(TEXT("gravity"), NULL, SND_ASYNC|SND_FILENAME);
    Sleep(2500);
    cout << "Here we go. You like this song? Or do you want me to turn it off?\n";
    Sleep(2000);
    cout <<"Wait.\n";
    Sleep(2000);
    cout << "Don't tell me yet. I'm grooving.\n";
    Sleep(7000);
    cout << "Okay, tell me what to do, " << name << "." << endl;
    getline(cin, song);

    if (song.find(str2)){
        cout << "Thank you! I, personally, love this song.\n";
        cout << "I probably wouldn't listen to you if you asked me to turn it off.\n";
        Sleep(20000);
    }
    else if (song.find(str3)){
        cout << "It's not coming off, " << name <<"." << endl;
    }

    return 0;
    }

return 0;
}

Aucun commentaire:

Enregistrer un commentaire