Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program is a game guess the right number you have different levels but as you can see when you run this the counter is not stopping the game I cant see why that is and I would like to offer the choice of playing it again as many times as you want and I am not sure where or how to do this any help would be appreciated thanks

#include "iostream"

using namespace std;

const int EASY = 1;
const int INTERMEDIATE = 2;
const int HARD = 3;

int main(){
	int choice;
	int answer;
	int counter = 0;

	cout << "If you want to win this new backpack you will have to play the game /n";
	cout << "The game is played by you guessing a number while I will tell you higher or lower until you get the right one /n";
	cout << "Chose your level /n";

	//Dispaly choice

	cin >> choice;
	if (choice == EASY)
	{
		while (counter = 15)
			cout << "You have 15 chances" << endl;
	}
   else if (choice == INTERMEDIATE)
	{
		cout << "You have 10 chances" << endl;
	}
   else if (choice == HARD)
	{
		cout << "You have 5 chances" << endl;
	}
   else 
	{
	
	}

	while (true)
	{	
		cin >> answer;
		//Display response
		if (answer == 35)
		{
			cout << "Your a winner /n";
			break;
		} 
		else if (answer > 35)
		{
			cout << "Lower /n)" << "Try Agin";
		} 
		else 
		{
			cout << "Higher /n)" << "Try again";	
		}
	}
	system ("pause");
	return 0;
}
Posted
Updated 11-Oct-10 16:52pm
v4

1 solution

Change your if-elseif-else branch to store counter the desired value. You are just storing the value and looping over that.

while (counter = 15)
    cout << "You have 15 chances" << endl;


The above will result in infinite loop.

Change it like following. Do not loop right in the if-else branch. Just store the counter.

if (choice == EASY)
{
    counter = 15;
    cout << "You have 15 chances" << endl;
}
    else if (choice == INTERMEDIATE)
{
        counter = 10;
        cout << "You have 10 chances" << endl;
}
    else if (choice == HARD)
{
    counter = 5;
        cout << "You have 5 chances" << endl;
}



This will give the user infinite chances to play single game
while(true) 
{


but the following will give counter number of chances for single game but infinite number of games to play.

while(true)
{
    while (counter--)
    {
        if(correct)
           break;
        else if (lower)
           cout << "lower";
        else
           cout << "Higher";
    }
    
    cout << "Do you want to play again? Y/N";
    cin >> Choice;
    
    if(Choice == 'N')
       break;   
}
 
Share this answer
 
Comments
EDITH GINGRAS 12-Oct-10 22:52pm    
thanks but I am still not understanding where all this code goes into the program I am not getting this at all for some reason and even though I have read this stuff its not clicking in my head can you explain this a little clearer for me I would really appreciate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900