Why are my or operators not working?

Hey guys i am very new to c++and started doing some of the beginner projects found on this forum. in the following code. can someone explain to me why i can not get the operators to work in this loop. i have looked far and wide and can not seem to find an answer to why this isn't working correctly. please help!



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
28
#include <iostream>

using namespace std;

int main()
{
int (x);
int chance=0;

        std::cout<<"****Welcome to the Number Game****\nInstructions: Enter any
 number you like, except for the number 5.\n""If you enter the number 5 you will 
automatically Lose. Lets see how long you Last\n";

	while(x!=5 || chance>10){
	   std::cout<<"Please Enter a Number \n";std::cin>>x;chance=chance+1;
}
	if(x=5){
		std::cout<<"You Broke the Rules!\nYOU LOSE!!!";
}
	else{
		std::cout<<"YOU WIN!";
}




		return 0;
}
Last edited on
I have moved Brackets changed to different loops but if i remove the or operator and chance >10 it will just keep going till i hit 5 but wont work with the chance section there.
Last edited on
I guess on line 17 you actually mean if(x==5)
You can easily avoid these mistakes by setting your compiler warnings to the highest level and look at the compiler output.
The if(x==5) part works but the chance>10 part doesnt work so the else statement never runs the while loop just keeps running until you 5. also after chance does = 10 hitting 5 wont even end the loop
Last edited on
You need to think about the conditions in your while loop.
The game is over if x == 5 or chance > 10
Do you really feel comfortable in that cramped style? Well, it's truly personal, I guess:
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
28
29
30
31
#include <iostream>

int main()
{
    std::cout << "****Welcome to the Number Game****\nInstructions: Enter any "
                 "number you like, except for the number 5.\n"
                 "If you enter the number 5 you will automatically lose.\n"
                 "Lets see how long you last!\n";

    int x = 0; // not "int (x);"
    int chance = 0;
    /* ----------------------------------------------------------------------
     * Thomas1965:
     * You need to think about the conditions in your while loop.
     * The game is over if x == 5 or chance > 10
     * ---------------------------------------------------------------------- */
    while(x!=5 || chance>10) {
        std::cout << "Please Enter a Number ";
        std::cin >> x;
        std::cin.ignore(1);
        chance = chance + 1;
    }
    if(x == 5) {
        std::cout << "You Broke the Rules!\nYOU LOSE!!!\n";
    }
    else {
        std::cout << "YOU WIN!";
    }

    return 0;
}

while(x!=5 && chance<=10) { // 11 iterations?

or

1
2
3
4
5
6
7
8
for(int chance = 0; chance<=10; ++chance) { // 11 iterations?
        std::cout << "Please Enter a Number ";
        std::cin >> x;
        std::cin.ignore(1);
        chance = chance + 1;
if(5 == x)
  break;
    }
http://www.cplusplus.com/forum/articles/12974/
this is the page where i got the project idea from, i am on the third project on the list. its called While( user == gullible ).
You need to think about the conditions in your while loop.
The game is over if x == 5 or chance > 10
that is exactly what i want it to do and its not, and that is my problem.

@coder777
while(x!=5 && chance<=10) { // 11 iterations?
my original code started i with chance = 1 but i switched to a "do while" loop and had to put the zero so it was 10- tries and not 9, but whats actually happening is after chance = 10 hitting 5 wont even end the program, instead of ending the loop in makes it an infinite loop with no exit.

PLEASE HELP MY BRAIN IS GOING TO EXPLODE! i just cant find why the it doesnt work right all seems to be written correctly.
While( user == gullible ) [...] PLEASE HELP MY BRAIN IS GOING TO EXPLODE!

What an inspired name! Isn’t it? :-)
Everyone who’s giving you a help today got through the same sensation more then once in the past, me the first.

Short answer:
coder777 has already given you the right solution.

Long answer:
Thomas1965 gave you the decisive hint:
Thomas1965 wrote:
The game is over if x == 5 or chance > 10

That means:
- your loop will stop running if user’s answer is 5 *or* the attempt number is more then 10.
Actually what you need is the condition to *keep* the loop running, which of course is the opposite of the above:
- if you want your loop to run, *both* the user’s answer must be not 5 *and* the attempt number must be below 10.

After you’ll have solved your “while” issue, you could try to guess what coder777 was also emphasising:
if you start counting from 0 and you get to 10, how many attempts would you count?
;-)

What an inspired name! Isn’t it? :-)


honestlly i didn't even notice that code777 changed the code in the second set of code brackets. i just thought he was showing me it was 11 chances i had setup and not 10. so thank you for pointing that out and thank you code777 for the answer. it is just weird the way i had it will break the loop once i hit the 10 (or 11) iterations of the loop it just kept on going. is there a reason that the or operator did not seem to be working. i understand the way code777 did it made it way easier than i was doing it, but is there a reason that my loop didnt work properly. as far as i know everything in the code looks right just not working as intended.

But again i thank everyone who took the time to check out my code. as a beginner i love finding active forums with people who actually help.
while(x!=5 || chance>10)
is there a reason that my loop didnt work properly

Your conditional instruction states:
“This loop must keep on running while *at least one* of these condition is true:
- the user’s answer is different from 0;
- the attempt number is more then ten.”
Actually the first condition keeps true until the user opts for a 5, so the loop goes on running, because it just needs one of the two to be true.
That’s how ‘or’ works.

Note: please, if you don't need further help, mark the post as solved.
Last edited on
Your conditional instruction states:
“This loop must keep on running while *at least one* of these condition is true:


I understand how it works. The way i have it posted i assumed: if one of those statements is true it will exit the loop but when it hit attempt number ten you get stuck in the loop and it wont move on unless you close the program itself. even if you hit the number 5 after the tenth time, the loop continues. my question is more of why does only have of the loop conditions work for only part of the program. entering the number 5 works fine until i get to my tenth attempt after that it just keeps asking for numbers with no end.

Also, i didnt realize i had to mark the page as solved, but i was still kinda looking for why my version didnt work right, so i dont make that mistake again. and with all the comments i assume that every thinks i dont know what its supposed to do, which i do, i just dont know why it isn't working the way i originally coded it.

if i should make a seperate post asking why it doesnt work i will do that. sorry bout the constant questions. but i have only been coding for 2 days and before i move on i like to know what went wrong so i dont do it again!
Last edited on
Ok so now there is a new problem. the code that was given to me from Code777
1
2
3
4
5
6
7
8
9
10
11
12
while(x!=5 && chance<=10) { // 11 iterations? 

or

for(int chance = 0; chance<=10; ++chance) { // 11 iterations?
        std::cout << "Please Enter a Number ";
        std::cin >> x;
        std::cin.ignore(1);
        chance = chance + 1;
if(5 == x)
  break;
    }


does not work either. i tried my own version and then copied his version to make sure it wasnt just me but when i try it, i enter one number and program ends. so doesnt matter what number just ends the program all together. so what is next? Please help and thanks for the help so far!
Finally i have figured it out. it was a couple of misplaced closing brackets, and i added the break at the end of each if statement. and the fact that it wasnt my code made me happy and the fact that it was such an easy fix made me angry lol i appreciate all the help. it kept me going and motivated to find out why this wasnt working. also after trying the code from code777 im sorry to say did not work at all but thanks for trying and i am glad i mark as solved until i got everything running propperly. thanks again and here is the final code.

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
28
29
30
#include <iostream>

using namespace std;

int main()
{
int (x);
int chance=1;

        std::cout<<"****Welcome to the Number Game****\nInstructions: Enter any number you like, except for the number 5.\n"
        "If you enter the number 5 you will automatically Lose. Lets see how long you Last\n";
        
        
	while(x!=5 || chance<10){//while x doesn't = 5 or you are not on you 10th attempt loop should continue
		std::cout<<"Please Enter a Number - \n";std::cin>>x;chance=chance+1;


	if(x==5){//if x=5  print statement below on screen
		std::cout<<"You Broke the Rules!\nYOU LOSE!!!";
		break;}

	if(chance > 10){//after your 10th attempt Print statement below
		std::cout<<"YOU WIN!";
		break;}
}//while loop closing bracket



		return 0;
}
code from code777 im sorry to say did not work
It actually does work. You copy/paste it wrongfully:

1
2
3
4
5
6
7
8
for(int chance = 0; chance<10; ++chance) {
        std::cout << "Please Enter a Number ";
        std::cin >> x;
        std::cin.ignore(1); // This line is unnecessary
        // Note, remove: chance = chance + 1;
        if(5 == x)
          break;
    }


Your loop condition is actually wrong.

Because of the || It will loop at least until chance<10. After that it it will check whether x!=5. If that is not the case the loop ends. for || each conditions must return false so that the result is false.

See Logical operators:

http://www.cplusplus.com/doc/tutorial/operators/
Topic archived. No new replies allowed.