May 5, 2015 at 8:57pm UTC
I've been trying to get this code to work, but when i build and run it, it doesnt matter what name is inputted. As if the "if" statement didnt exist.
Would greatly appreciate some help with this! :)
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name = "" ;
cout << "What is your name?\n" ;
cin >> name;
if (name == "Alice" || "Bob" )
{
cout << "Hello" << name << ".\n" ;
}
else
{
cout << "Bye" << name << ".\n" ;
}
}
Last edited on May 5, 2015 at 8:58pm UTC
May 6, 2015 at 1:28pm UTC
Ohh, i understand! Thank you so much for helping! ^.^ i Will redo the code once i get home from school.
May 6, 2015 at 3:32pm UTC
@ vaiandrept4, that will not work.
'name' can not be 'Alice' AND 'Bob' at the same time.
May 6, 2015 at 3:39pm UTC
Actually i tested it and it work, but the && in this case means "or" too.
May 6, 2015 at 3:41pm UTC
Very strange.
Last edited on May 6, 2015 at 3:42pm UTC
May 6, 2015 at 4:31pm UTC
I bet what's happening is that it evaluated like this: if ((name) == ("Alice" && "Bob" ) == (name))
This can be corrected by using parenthesis to emphisize the order of operations that you want to have happen.
It makes code more readable, but also removes room for error.
Last edited on May 6, 2015 at 4:33pm UTC
May 6, 2015 at 6:22pm UTC
wtf? Just write it like AbstractionAnon stated in the first reply. It's clear and concise.
Last edited on May 6, 2015 at 6:23pm UTC