#include <iostream>
using namespace std;
int main ()
{
    int R,G,Y;
    int r,g,y;
    int a;

    cout<<"enter your input"<<endl;
    cin>>a;

    if (a==R||r)
    {
        cout<<"please stop"<<endl;
    }
    else 
        if (a==G||g)
        {
        cout<<"you can go"<<endl;
        }

        else if (a==Y||y)
        {
        cout<<"prepare to stop"<<endl;
        }
        else 
        cout<<"Out of range"<<endl;

return 0;
}

Recommended Answers

All 5 Replies

else if (a==Y||y)

What exactly are you trying to do?
else if (a == 1 ) ???

You really need more description of what you're trying to do!

R,G,Y, r,g,y aren't even initialized!

your boolean logic is ill-formed inside of your if statements, instead of

if(a==R||r)
//etc.

it should be

if((a == R) || (a == r))
//etc.

how your first logic reads is "if a equals R or if r".
how I think you wanted your logic to read is "if a equals R or if a equals r"

but yes, as wildgoose has mentioned also: r, R, g, G, y, Y will all be the compiler default value of int, they are uninitialized thus making them unsafe for comparisons.

hope that's helpful

~J

if ( ReadBeforePosting() == true && PostHasCodeTags() == true ) {
  // reward OP with answers
} else {
  MoreCluesOnHowToDoBetterNextTime();
}

In C++, if you compare something like R WITHOUT any quotes, it interprets it as a variable. So, I assume that the user will enter R or r. If so, you need to check like

if (input == 'R')
{
}

or like this

if (input == "stop")
{
}

Double quotes are usually used for more than one characters but it doesn't really matter.... Also, read the post that says your boolean logic is incorrect.

your boolean logic is ill-formed inside of your if statements, instead of

if(a==R||r)
//etc.

it should be

if((a == R) || (a == r))
//etc.

how your first logic reads is "if a equals R or if r".
how I think you wanted your logic to read is "if a equals R or if a equals r"

but yes, as wildgoose has mentioned also: r, R, g, G, y, Y will all be the compiler default value of int, they are uninitialized thus making them unsafe for comparisons.

hope that's helpful

~J

Thank you so much..
:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.