Guessing Game - So Confused

Hello. I am in my first c++ class, and was doing very well. I missed a class and I am completely lost. I am reading through the book and some of the different loops confuse me. Could I get some advice with the following code? I have to make a game similar to that below. I need to make it a best of 7 game. I am not sure how to make the program repeat the question and allow the user the input their guess. Each time the question is asked the Correct or Incorrect count will increase by one, until the user has got 4 correct answers or 4 incorrect answers. My instructor said the do while loop would be useful.



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
#include <iostream>
#include <cstdlib>  // For rand and srand
#include <ctime>	// For the time function
using namespace std;

int main()
{
	int Guess1;
	cout << "Welcome to the guessing game!  Press Enter if you would like to play. " << endl;
	cout << "I am thinking of a number from 1 to 4. " << endl;
	cout << "What is your guess? ";
	cin >> Guess1;
	if (Guess1 > 4 || Guess1 < 1)
	{
		cout << "Error: Please guess a number between 1 and 4. " << endl;
	}

	srand(time(0));
	int RanNum;
	int NumberCorrect = 0, NumberIncorrect = 0;
	for (int i = 0; i <1; i++)
	{
		RanNum = rand() % 4 + 1;
	}
	
	if (Guess1 != RanNum)
	{
		cout << "Sorry! I was thinking of " << RanNum << endl;
		++NumberIncorrect;
	}
	
	if (Guess1 == RanNum)
	{
		cout << "Correct! I was thinking of " << RanNum << endl;
		++NumberCorrect;
	}

	cout << "Correct - " << NumberCorrect << "\t Incorrect - " << NumberIncorrect << endl;

	system("pause");
	return 0;
}
First, generate a random number.
then ask for the player's guess, like this:

1
2
3
4
5
6
7
8
9
10
11
int guess;
int tries = 0;
srand(time(0));
int RanNum = rand() % 4 + 1;
do
{
cout<<"Enter your guess: \n";
cin>>guess;
++tries
// and so on....
}



here's a full Guess My Number game i've written for you:
(You can ignore the functions if you haven't learned them yet)

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
67
68
69
70
71
72
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

char AskYesNo(string);
void instructions();

int main()
{
	cout<<"\t\t\t\tGuess My Number";
	cout<<"\n\t\t\t\t----------------\n\n";
	cout<<"Welcome to Guess my number,\n";
	char answer = AskYesNo("Would you like to see some instructions before the game starts? (y/n)\n");
	if ( answer == 'y' )
	{
		instructions();
	}
	else
	{
		cout<<"Very well.\n\n";
	}

	srand(static_cast<unsigned int>(time(0)));
	int randomNumber = rand() % 100 + 1;
	int tries = 0;
	int guess;
	do
	{
		cout<<"Please enter a guess: \n";
		cin>>guess;
		++tries;

		if ( guess > randomNumber )
		{
			cout<<"Too High!\n";
		}
		else if ( guess < randomNumber )
		{
			cout<<"Too Low!\n";
		}
		else
		{
			cout<<"That's it! you've successfully guessed the secret number in " <<tries<< " tries!\n";
		}
		if ( tries > 10 )
		{
			cout<<"Game over - You failed to guess the number.\n";
			break;
		}
	} while ( guess != randomNumber );
	system("pause");
	return 0;
}
	
char AskYesNo(string question)
{
	char respond;
	cout<<question<<endl;
	cin>>respond;
	return respond;
}

void instructions()
{
	cout<<"Instructions:";
	cout<<"\n-------------\n\n";
	cout<<"The Game is very simple, the computer will guess a number between 1 - 100.\n";
	cout<<"It's your job to guess that number, you only have 10 tries.\n";
	cout<<"Best of luck!\n\n";
}

Last edited on
This is very similar to my own. I just ran it and I am still confused with one thing. How do I make it a best of 7 game. My code runs once, and your code runs until the number is guessed. I want to make the player win after guessing atleast 4 correct answers, or loose after guessing wrong atleast 4 incorrect answers.
I'm new at this too and am at modular sequence structures, sorry I don't have any advice. I have a question though: How did you do the screenshot?
Thanks.
Hi Anna, this isn't really a screenshot it's a souce code format, you can see there's a little board to the right called 'Format' highlight your code and click the <> button (The souce code button)
I think you want something like this? It runs until NumberCorrect = 4 or NumberIncorrect = 4

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
#include <iostream>
#include <cstdlib>  // For rand and srand
#include <ctime>	// For the time function
using namespace std;

int main()
{
	
	srand(time(NULL)); // moved this to the start of main, put NULL instead of 0 in the brackets
	int Guess1;
	int NumberCorrect = 0, NumberIncorrect = 0; // moved this declaration
	int RanNum; // moved this declaration
	
	cout << "Welcome to the guessing game!  Press Enter if you would like to play. " << endl;
	
	do // added this do while loop
	{
		cout << "I am thinking of a number from 1 to 4. " << endl;
		cout << "What is your guess? ";
		cin >> Guess1;
		if (Guess1 > 4 || Guess1 < 1)
		{
			cout << "Error: Please guess a number between 1 and 4. " << endl;
		}

		RanNum = rand() % 4 + 1; // removed the for loop around this, not sure why you needed it??
		
		if (Guess1 != RanNum)
		{
			cout << "Sorry! I was thinking of " << RanNum << endl;
			++NumberIncorrect;
		}
		
		if (Guess1 == RanNum)
		{
			cout << "Correct! I was thinking of " << RanNum << endl;
			++NumberCorrect;
		}
	} while(NumberCorrect != 4 && NumberIncorrect != 4);

	cout << "Correct - " << NumberCorrect << "\t Incorrect - " << NumberIncorrect << endl;

	system("pause");
	return 0;
}
Avoid magic numbers. http://sourcemaking.com/refactoring/replace-magic-number-with-symbolic-constant

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
#include <iostream>
#include <cstdlib>  // For rand and srand
#include <ctime>	// For the time function

int main()
{
    const int MIN = 1 ; // minimum value of random number
    const int MAX = 4 ; // maximum value of random number
    const int TARGET = 4 ; // number of correct or incorrct answers to end the game

    std::srand( std::time(nullptr) ) ; // seed he lcg just once, at the beginning

    std::cout << "Welcome to the guessing game!\n" ;

    int correct = 0 ; // number of correct answers
    int incorrect = 0 ; // number of incorrect answers

    // repeat until the user has got TARGET (4) correct answers or TARGET incorrect answers
    while( correct < TARGET && incorrect < TARGET )
    {
        // generate a random number in [MIN,MAX]
        const int ran_num = std::rand() % (MAX-MIN) + 1 ;

        std::cout << "\nI am thinking of a number from " << MIN << " to " << MAX
                  << ".\nWhat is your guess? " ;
        int guess ;
        std::cin >> guess ;

        if( guess > MAX || guess < MIN  )
        {
            std::cout << "Error: Please guess a number between "
                      << MIN << " and " << MAX << '\n' ;
        }

        else if( guess == ran_num )
        {
            std::cout << "Correct! I was thinking of " << ran_num << '\n' ;
            ++correct ;
        }

        else
        {
            std::cout << "Sorry! I was thinking of " << ran_num << '\n' ;
            ++incorrect ;
        }

        std::cout << "So far: Correct - " << correct << "\t Incorrect - " << incorrect << '\n' ;
    }

    std::cout << "\n\nGame over: Correct - " << correct << "\t Incorrect - " << incorrect << '\n' ;
}
Last edited on
Okay this is what I have so far. I am almost done. I just have to have the game give the user a hint only after 2 incorrect guesses. The hint being whether the number is even or odd.

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
	int RanNum; 
	int NumberCorrect = 0, NumberIncorrect = 0;

	cout << "Welcome to the guessing game! " << endl;
	cout << "Choose Wisely: This is a best of seven game. " << endl;
	cout << "Press Enter if you would like to play. " << endl;
	
	do 
	{
		cout << "" << endl;  // Space
		cout << "I am thinking of a number from 1 to 4. " << endl;
		cout << "What is your guess? ";
		cin >> Guess1;
		if (Guess1 > 4 || Guess1 < 1)
		{
			cout << "Error: Please guess a number between 1 and 4. " << endl;
		}
		
		RanNum = rand() % 4 + 1; // Makes RanNum between 1 and 4
		if (Guess1 != RanNum)
		{
			cout << "" << endl;
			cout << "Sorry! I was thinking of " << RanNum << endl;
			++NumberIncorrect;
		}
		
		if (Guess1 == RanNum)
		{
			cout << "" << endl;
			cout << "Correct! I was thinking of " << RanNum << endl;
			++NumberCorrect;
		}

	} while(NumberCorrect != 4 && NumberIncorrect != 4);
	
	cout << "Correct - " << NumberCorrect << "\t Incorrect - " << NumberIncorrect << endl;
	
	{
	if (NumberCorrect >= 4) { cout << "You Win!" ;  // Not sure why the program didn't build when I wrote if (NumberCorrect == 4)
							  cout << "" << endl;}  //  
	
	if (NumberIncorrect >= 4) { cout << "You Loose!" ;  // Same as above with (NumberIncorrect == 4)
								cout << "" << endl;}   // Greater than worked

	}
	
	system("pause");
	return 0;
}



I am thinking it would be something like this, but I am not sure where to put it.

1
2
3
4
5
6
7
8
 
if (NumberIncorrect == 2)
{
cout << "I will offer you a hint.  The number is even." << endl;
}

But I am not sure what I need to add to determine what the random number is.
Why are you asking the user to guess before generating the random number? It would make more sense to generate RandNum at the start of the do while loop, and then prompt for the guess.

Then you could offer the hint after the "I am thinking of a number from 1 to 4" statement if 2 incorrect guesses have been made. But you will need to determine of the current random number is even or odd and then offer the appropriate hint.

Also, the "Press enter if you would like to play" statement is pointless as it starts anyway.

And I think you mean 'lose' not 'loose' :)
Last edited on
Also, do you want an illegitimate guess (not in the range 1-4) to be counted as an incorrect guess?

If not you can add continue; after the "Error, please enter a number between 1 and 4. " to skip to the next iteration of the do while loop and not count the illegitimate guess as an incorrect guess
Last edited on

#include<iostream>
#include<ctime>


using namespace std;

int main()
{
const int MAX = 20;
int won = 0;
int lost = 0;
int guessCount = MAX;
int guess;
char playAgain='y'; // Declares character
srand(time(NULL)); // Sets random number different each time
int number = rand() % 101; // Set the random number to 0-100

do
{
cout << "Im thinking of a number between 0-100. Take a guess: ";
cin>> guess;

while ( guessCount > 0)
{
guessCount--;
int num;
if (guess == number)
{
num = 'a';
guessCount = -1;
won++;
}
else if (guess > number)
{
num = 'b';
cout <<endl << "You have " << guessCount << " " << "tries left." <<endl;
}
else if (guess < number)
{
num = 'c';
cout <<endl << "You have " << guessCount << " " << "tries left." <<endl;
}

if(guessCount==0)
{
num='d';
lost++;
}
switch (num)
{

case 'a' : cout << "************************" << endl;
cout << "*Congrats!! You got it.*" <<endl;
cout << "************************" << endl;
break;
case 'b' : cout << "Too high,Guess again: ";
cin >> guess;
break;
case 'c' : cout << "Too low,Guess again: ";
cin >> guess;
break;
case 'd' : cout<<"********************" << endl;
cout<<"*Sorry but you lost*" << endl;
cout<<"********************" << endl;;
break;
default : cout << "Something is broken";
break;
}


}
cout << "Would you like to play again? y/n ";
cin >> playAgain;
guessCount=MAX;
if (playAgain == 'n')
{
cout<<"You won "<<won << " many times!" <<endl;
cout<<"You lost "<<lost<<" many times!"<<endl;
cout<<"For a total of "<<won+lost<<" games";
cout<<endl<<"Press any key to continue.";
system ("PAUSE");
}


}while (playAgain == 'y') ;
return 0;
}



Topic archived. No new replies allowed.