need a little assist with small bump in the code!

I am trying to set up a statement that will let me show that the user has run out of attempts for this code guessing game.

I am running into trouble however. I am sure it is something with my turncounter and the actual numbers but I can quite figure it out!

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
int turncounter = 0;
	
    while(turncounter != 10){
		turncounter++;

		cout << "Current try: " << turncounter << endl;

		for(int i=0;i<5;i++){
			cout << "What color is color #" << i+1 << "? "; 
			cin >> usercolors[i];
			cout << endl;
		}

		for(int i=0;i<5;i++){		
			if(usercolors[i] == colors[i])
				{cout << usercolors[i] << " is correct." << " ";}
		    else if(usercolors[i] != colors[i]) {
                 cout << usercolors[i] << " is wrong." << " ";}

       }
			

		cout << endl << endl;

		if (usercolors[0] == "Laura" &&
		   usercolors[1] == "Laura" &&
		   usercolors[2] == "Laura" &&
		   usercolors[3] == "Laura" &&
           usercolors[4] == "Laura") {
              cout << "Cheat activated, showing answer...\n";
                 
                 for(int i=0;i<5;i++){
                 cout << colors[i];
            }
              cout "\n\n";
        }
        
        if(usercolors[0] == colors[0] &&
		   usercolors[1] == colors[1] &&
		   usercolors[2] == colors[2] &&
		   usercolors[3] == colors[3] &&
           usercolors[4] == colors[4])
		{
		   cout << "You win!  It only took you " << turncounter << "tries \n";
		   break;
		}
  
    if(turncounter > 10) {
                   
        cout << " You have run out of attempts!"; 
                  
	}
    
    

}	
}
This cout << " You have run out of attempts!"; will only show if turncounter is greater than ten, but your code stops incrementing turncounter before it gets that high.
Ok, I have tried changing the number. both in the while loop condition and the attempts if statement and still having trouble. If I change the while condition to a high number, and turncounter > 10 if allows me to go to 12 tries still...

if I change the if statement to an = condition instead of greater, it prints immediately after the first try, no matter what.
Change the while loop to a for loop. It will make the code clearer.

After the loop, check whether turnCounter==10. If it does then the loop exited after 10 turns. If it doesn't, then the loop exited from the break statement because the user won the game.
1
2
3
4
5
6
7
8
9
10
for (turnCounter = 0; turnCounter < 10; ++turnCounter) {
    ....
    if (user won) {
	cout << "You win!  It only took you " << turncounter << "tries \n";
	break;
    }
}
if (turnCounter == 10) {
    cout << "You have run out of attempts\n";
}

Topic archived. No new replies allowed.