Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
int main(int argc, char** argv) 
{
    string stopLightColor = "" ;
    cout << "Please enter what your stoplight color is: " << endl ;
    cin >> stopLightColor;
    if (stopLightColor == "red" || "yellow") {
        cout << "Please stop." << endl ;
    }
    else if (stopLightColor == "green") 
    {
        cout << "Continue" << endl ;
    }

    return 0 ;
}


No matter what I type in, the answer always is "Please stop."

What I have tried:

I haven't necessarily tried anything new because I don't know what I could fix about it.
Posted
Updated 21-Apr-16 20:45pm
v2

Quote:
What am I doing wrong in my program?
Because you don't know the C++ syntax.
Learn really C++ by reading documentation and follow tutos.
Pay particular attention to logical operators.
 
Share this answer
 
v2
Comments
Member 12477055 21-Apr-16 22:40pm    
That's what I need help with. What is the error? :p
Sergey Alexandrovich Kryukov 22-Apr-16 0:12am    
I disagree. This is not about knowing the syntax, this is worse: not trying to think logically.
Moreover, formally, the syntax is correct. Correct solution is Solution 2. (I did not vote this time.)
—SA
As others already suggested you misunderstood how conditional expressions works in C++. That said, I suppose there ways to improve your code. I suppose there are two scenarios:

(1) You trust the semaphore (the user input) to be either 'red' or 'yellow' or 'green'.
In such scenario, the code could be written simply
C++
if (stopLightColor == "green")
{
  cout << "Continue" << endl;
}
else // it IS either 'red' or 'yellow', don't bother to check
{
  cout << "Please stop" << endl;
}


(2) You allow the semaphore (the user input) to be broken, at times.
In such a scenario you HAVE to handle the 'broken' eventuality.
C++
if (stopLightColor == "green")
{
  cout << "Continue" << endl;
}
else if ( stopLightColor == "yellow" || stopLightColor == "red")
{
  cout << "Please stop" << endl;
}
else // the semaphore is broken
{
  cout << "Warning: the semaphore is broken, please go on very carefully" << endl;
}
 
Share this answer
 
As ppolymorphe has suggested, look at this line

if (stopLightColor == "red" || "yellow")


can you think of the correct way it should be written ?
 
Share this answer
 
Comments
Member 12477055 21-Apr-16 22:43pm    
Only thing I can think of is getting rid of the "". I feel like the or operator is in the correct spot.
Garth J Lancaster 22-Apr-16 0:00am    
the "" are fine, and the or operator IS in the correct spot

the issue is,

a == b || c

the || c always gives you a false

so you need to write

a == b || a == c

get it ? (thats as close as I can without actually typing it for you)
Sergey Alexandrovich Kryukov 22-Apr-16 0:11am    
Correct, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900