I want it so that when a user doesn't enter char r,p,g,m,c,y it will come up and say incorrect input enter 4 correct values. This is what I have.

while( (g.one || g.two || g.three || g.four) != ('r' || 'p' || 'g' || 'm' || 'c' || 'y') );

It will not run the loop if I put in a char out of the range but if I change it to == and input one of those char it will run the loop. Why is that?

Recommended Answers

All 8 Replies

while( (g.one != 'r') || (g.two != 'p') || (g.three != 'g') || (g.four != 'm'))

that's how you give multiple values, you can modify it to suit your requirement

('r' || 'p')==1
You need to change approach to this problem, || is just logic or.

Firstly, your evaluations of boolean expressions are incorrect.

Rule of thumb: boolean logic is handled per expression, or as a stand-alone TRUE/FALSE flag.

In your loop condition, you are trying to handle everything at once. It is important that each individual expression be able to resolve to true or false.

I would code you up an efficient way to handle your loop condition; however, without seeing your code, anything that I give you at this point would be based on speculation.

while( (g.one != 'r') || (g.two != 'p') || (g.three != 'g') || (g.four != 'm'))

that's how you give multiple values, you can modify it to suit your requirement

well I want it so that if (g= guesses) any of those guesses are not equal to either r p g m c or y then it goes through the loop.

while( (g.one != ('r' || 'p' || 'g' || 'c' || 'y') ) || ....

I don't want to do that for every guess. Isn't there a more efficient way?

Firstly, your evaluations of boolean expressions are incorrect.

Rule of thumb: boolean logic is handled per expression, or as a stand-alone TRUE/FALSE flag.

In your loop condition, you are trying to handle everything at once. It is important that each individual expression be able to resolve to true or false.

I would code you up an efficient way to handle your loop condition; however, without seeing your code, anything that I give you at this point would be based on speculation.

hmmm so I would have to do it the way the Agii said. :/ I can't just mush it all together XD. My code is over 400 lines long :c I don't think you would want to read all of it.

you can write a function to check if the value is equal to any of those given values and return a bool value

for example:

bool checkValue(char c)
{
    return ((c == 'r') || (c == 'p') || (c == 'g') || (c == 'm') );
}

//and then call it in the while loop like

while(checkValue(g.one) || checkValue(g.two) || checkValue(g.three))
{
 //do something
}
commented: helped me with do while problem +1
commented: Excellent suggestion :) +25

you can write a function to check if the value is equal to any of those given values and return a bool value

for example:

bool checkValue(char c)
{
    return ((c == 'r') || (c == 'p') || (c == 'g') || (c == 'm') );
}

//and then call it in the while loop like

while(checkValue(g.one) || checkValue(g.two) || checkValue(g.three))
{
 //do something
}

ok cool. Thx!

you can write a function to check if the value is equal to any of those given values and return a bool value

for example:

bool checkValue(char c)
{
    return ((c == 'r') || (c == 'p') || (c == 'g') || (c == 'm') );
}

//and then call it in the while loop like

while(checkValue(g.one) || checkValue(g.two) || checkValue(g.three))
{
 //do something
}

Hmmmm I tried it doing it, but it is doing the same thing as it did before. here is my code

while( (checkValue(g.one)) && (checkValue(g.two)) && (checkValue(g.three)) && (checkValue(g.four)) );

bool checkValue(char c)
{
return ( (c != 'r') || (c != 'p') || (c != 'g') || (c != 'm') || (c != 'c') || (c != 'y') );
}

I don't think it changes anything but the while loop is a do while loop. This works with == like before and the only way to get out of the loop is by not inputing those numbers which is the opposite of what I want. When I add not r or p or g.... When I run it all cases are false and continue looping around in the do while loop :/.

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.