looping only 5 times?!

I'm having trouble. I'm not very good with this but I'm trying to get this to loop only 5 times. I don't have to have to enter a code or a certain number in order to get it to finish collecting data. It's suppose to be where you have 5 guesses to get it right! Can anyone help me

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
//Guessing Game Project
//Simulates a number guessing game
//Created/revised by Christopher Hart

#include <iostream>
#include <ctime>

using std::cout;
using std::cin;
using std::endl;

int main ()
{
	//variables
	int randomNumber = 0;
	int numberGuess = 0;

	//random number 1 through 50
	srand(static_cast<int>(time(0)));
	randomNumber = 1 + rand() % (50 - 1 + 1);

	//guess number
	cout << "Guess a number from 1 through 50: ";
	cin >> numberGuess;

	while (numberGuess != randomNumber)
	{
		cout << "Sorry, please try again: ";
		cin >> numberGuess;
	}
	cout << endl << "Yes, the correct number is " << randomNumber << "." << endl;
	
	return 0;
}	//end of main function 
Create a "counter" variable that increments each time the guess and add that condition to the while loop.
I was looking into it but i guess i'm still a little confused about the counter idea. I'm not sure how to integrate it into it.
Create an integer, then at the end of while loop, increment it by one. Then add a condition in the while loop that checks to make sure it is < however much you need it to be.
ok i was able to figure it out. it dawn on me shortly after my last post. I'm gone through and made some changes according to the assignment. however now I need to ask the user if they want to continue (Y or N) and restart the game until the user selects no. any ideas on how to tackle this?
create a bool flag (true/false) and set it to false when u initialize it. make a big loop(where all ur code that needs to be repeated goes in there) and set the condition to be while( flag != true). at the end of the loop, ask them if they want to exit the game. then make a if statement where if the answer equals yes, change the flag to true and the next time around the loop should recognise it and not go in it. If it does, just put a break in the if statement (poor practice but).
I'm surprised no one has mentioned a for loop yet.

replace your while loop with something like:

1
2
3
4
5
6
7
8
for (int i = 0; i < 4; i++)
{
     if (numberGuess != randomNumber)
     {
          cout << "Sorry, please try again: ";
	  cin >> numberGuess;
     }
}


In the for loop, the first declaration is the variable that is incremented every loop until it reaches the desired number of loops. The second statement is controls at which point the for loop will stop, the third increments your first variable until it reaches the point where the loop is broken.

Last edited on
Yeah, but it won't break the way you have it. If they guess the right number, then it will loop till the variable is 3 and then it will exit, either:

a.) making it so the variable i doesn't exist anymore, making it so you don't know how many guesses it took them
b.) i will always be 4 (3) at the end, making the number of guesses always the max possible
Topic archived. No new replies allowed.