+ 2

Why is my code executing one of my "if" statements when I'm entering the conditions of another?

string words; cin >> words; if ((words == "Hi") || (words == "hi")) { cout << "Hello!" << endl; } if ((words == "How are you?") || (words == "how are you?")) { cout << "I'm doing fine, thanks!" << endl;} I'm entering "How are you?" but the program is outputting "Hello!" Can anyone explain to me what's going on?

28th Sep 2016, 2:18 PM
Indie Geek
Indie Geek - avatar
7 ответов
+ 1
If you type any string this compiler will treat it as multiple inputs so it is better to assign the value to some variable and then execute it..
28th Sep 2016, 2:26 PM
Balaji Rs
Balaji Rs - avatar
+ 1
yes the user wouldn't be able to type "How are you" or Hi but this problem is only with this compiler..I think in turbo c++ we can enter string with space..
28th Sep 2016, 3:42 PM
Balaji Rs
Balaji Rs - avatar
0
thank you!
28th Sep 2016, 2:31 PM
Indie Geek
Indie Geek - avatar
0
But how can I assign the value of a string to another variable?
28th Sep 2016, 2:48 PM
Indie Geek
Indie Geek - avatar
0
instead of cin>>words; use words="Hi"
28th Sep 2016, 2:51 PM
Balaji Rs
Balaji Rs - avatar
0
But if I use words = "Hai", then the users wouldn't be able to input the word, would they?
28th Sep 2016, 3:30 PM
Indie Geek
Indie Geek - avatar
0
Thank you so much!
28th Sep 2016, 5:06 PM
Indie Geek
Indie Geek - avatar