0
#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;
}

Edited by Reverend Jim: Fixed formatting

5
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by renovat0
0

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!

0

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

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

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.