First C++ Attempt/Total Noob

Hey there, guys. Very first time doing any type of coding, literally started watching videos yesterday. So this is a very basic problem.. I've used google to try and find the answer to my woes but I can't find anything. The problem is with the code below - Instead of going to my 'for' loop, it immediatly bipasses it and head to the "You've won!"

Guessing it's something simple and quick. Thanks.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{
    int secretnumber = 76;
	int playerguess;
	int lives = 7;

	cout << "--------!Guess my number!-------- \n \n";
	cout << "!My number is between 0 and 100! \n-------------------------------- \n \n";
	
	for ( ; lives = 0 ; )
	{
		cout << "What do you think it is.... : ";

		cin >> playerguess;


		if (playerguess == secretnumber)
		{
			cout << "You got it! \n";
			cout << "You got it! \n";
			cout << "You got it! \n";
			cout << "You got it! \n";
			break;
		}
		else
		{
			cout << "\n";
			cout << "You didn't get it! \n \n";

			if (playerguess > secretnumber)
			{
				cout << "Lower! \n";
				lives -= 1;
				cout << "You have : " << lives << "\n";
			}
			else
				if (playerguess < secretnumber)
				{
					cout << "Higher! \n";
					lives -= 1;
					cout << "You have : " << lives << "\n";
				}
				else
					cout << "!This can't be possible! \n";
		}
	}
	
	if (lives = 0)
		cout << "You've lost! You ran out of lives!";
	else
	{
		cout << "You've Won! \n";
		cout << "----------- \n";
		cout << "!Well done! \n";
	}

	char f;
	cin >> f;
	return 0;
}
Use a for-loop, something like:

1
2
3
4
for( lives = 5; lives > 0; lives-- )
{
    /* code */
}


EDIT:
http://www.cplusplus.com/doc/tutorial/control/
Scroll about half way down.
Last edited on
for ( ; lives = 0 ; )

I expect you meant

for ( ; lives == 0 ; )

Likewise elsewhere.
Last edited on
As for an explanation of why it bypassed the for loop, here are the elements that cause the problem:
1
2
3
4
5
6
7
8
9
10
int a;
int b = (a = 7); //a and b are both 7 now, because (a = 7) returns a, thus b = a;

if(0){} //this condition is the same as if(false)
if(1){}
if(2){} //these three are the same as if(!false) or if(true)
if(3){}

for(; lives = 0;){} //because "lives = 0" returns lives, and lives is 0, the condition is false
//thus, the loop doesn't even run once. 
@Lynx876 Amended. Thanks.

@Moschops Should have noticed that, yeah. Thanks.

@L B Don't understand why that's a problem. Probably don't know one of the basic rules I suppose..? Surely it's not a problem to tell the For loop to stop if lives is 0 or False?
Possible for you to give a example of a way to fix this?
Last edited on
The problem is that you used "=" for assignment instead of "==" for equality check, as Moschops pointed out. I just explained how that affected it. ;)
LB is basically saying what Moschops said.

You're using assignment( = ) operators, instead of equality( == ) operators.

Anything other than 0, within an if-statement will be true, only if( 0 ) will be false.
Which is why L B wrote:
1
2
3
4
if(0){} //this condition is the same as if(false)
if(1){}
if(2){} //these three are the same as if(!false) or if(true)
if(3){}


Check line 54 of your code, you're assigning instead of checking for equality.

EDIT:
What he said ^^^^^ lol!
Last edited on
Ha. Ugh fuck. I hate feeling like a total noob.

I *hope* (almost certain) I have just understood what you've said. I've changed the coding to this, no positive results. Apologies.


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{
    int secretnumber = 76;
	int playerguess;
	int lives = 7;

	cout << "--------!Guess my number!-------- \n \n";
	cout << "!My number is between 0 and 100! \n-------------------------------- \n \n";
	
	for ( ; lives == 0 ; lives-- )
	{
		cout << "What do you think it is.... : ";

		cin >> playerguess;


		if (playerguess == secretnumber)
		{
			cout << "You got it! \n";
			cout << "You got it! \n";
			cout << "You got it! \n";
			cout << "You got it! \n";
			break;
		}
		else
		{
			cout << "\n";
			cout << "You didn't get it! \n \n";

			if (playerguess > secretnumber)
			{
				cout << "Lower! \n";
				cout << "You have : " << lives << "\n";
			}
			else
				if (playerguess < secretnumber)
				{
					cout << "Higher! \n";
					cout << "You have : " << lives << "\n";
				}
				else
					cout << "!This can't be possible! \n";
		}
	}
	
	if (lives == 0)
		cout << "You've lost! You ran out of lives!";
	else
	{
		cout << "You've Won! \n";
		cout << "----------- \n";
		cout << "!Well done! \n";
	}

	char f;
	cin >> f;
	return 0;
}
Last edited on
for( ; lives > 0; lives-- )

for( ; while lives are greater than 0( run statements within loop); take 1 from lives )
You need to change your for loop to what Lynx suggestion in the second post:

1
2
3
4
for( lives = 5; lives > 0; lives-- )
{
    /* code */
}


which:
- Sets lives to 5 (you want 7, so put 7)
- Loops over and over while lives is still above 0
- Reduces lives by 1 every loop

Currently your for loop runs while lives is 0, but you start with 7, so that's never.
@Lynx876

Brilliant. Perfecto. Thanks alot, I had it in my head that it exited the loop on the requirements of that middle section rather than staying in. Vastly appreciated.
Last edited on
No problem. From the link I gave above;
Its format is:

for (initialization; condition; increase) statement;
Yes, but it is often unclear from just that line whther the loop goes until condition is true or until it is false. ;)
Topic archived. No new replies allowed.