Please help me with this if / else if.

It always uses the else statement as true! I am beginner, just started today! Can someone explain?

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
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    char name[40], old[30], mood[30];

    cout << "Hello! What is your name?" << endl;
    cin >> name;
    cout << "How are you "<< name << "?" << endl;
    cin >> mood;

    if(mood == "good" || mood == "Good"){
        cout << "Thats great to hear " << name << "! So how old are you?" << endl;
    }
    else if(mood == "great" || mood == "Great"){
        cout << "Thats amazing " << name << "! So how old are you?" << endl;
    }
    else if(mood == "okay" || mood == "Ok"){
        cout << "Cool " << name << "! So how old are you?" << endl;
    }
    else if(mood == "bad" || mood == "Bad"){
        cout << "Aww, it gets better don't worry " << name << "! So how old are you?" << endl;
    }
    else if(mood == "awful" || mood== "Awful"){
        cout << "Aww, I am so sorry to hear.. It gets better " << name << "! So how old are you?" << endl;
    }
    else{
        cout << "Mhmm... So how old are you?" << endl;
    }
    return 0;
}
Welcome to C++. C++ has string that would be better suited for this job. http://www.cplusplus.com/reference/string/string/

Just be sure to include <string>.
Use std::string to hold sequences of characters; it makes life a lot easier.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

int main()
{
    // https://cal-linux.com/tutorials/strings.html
    std::string name, mood;

    std::cout << "Hello! What is your name? " ;
    std::cin >> name;
    std::cout << "How are you " << name << "? " ;
    std::cin >> mood;

    if(mood == "good" || mood == "Good"){
        std::cout << "Thats great to hear! " << name  << '\n';
    }
    else if(mood == "great" || mood == "Great"){
        std::cout << "Thats amazing! " << name  << '\n';
    }
    else if(mood == "okay" || mood == "Ok"){
        std::cout << "Cool! " << name  << '\n';
    }
    else if(mood == "bad" || mood == "Bad"){
        std::cout << "Aww, it gets better don't worry " << name  << '\n';
    }
    else if(mood == "awful" || mood== "Awful"){
        std::cout << "Aww, I am so sorry to hear.. It gets better " << name  << '\n';
    }
    else{
        std::cout << "Mhmm... " << '\n';
    }

    std::cout << "So how old are you? " ;
    int age ;
    std::cin >> age ;

    // return 0; // there is an implicit return 0 at the end of main
}
Topic archived. No new replies allowed.