need help with c++ guessing game

Jan 30, 2019 at 10:40pm
Write your question here.

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
// GuessingGame.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <cstdlib>
int exitP(int x) {

	std::cout << "\nWould you like to play again? [1 -> no :: 2 -> yes]: ";
	std::cin >> x;


	switch (x) {
	case 1: std::cout << "Goodbye!";
		exit(EXIT_SUCCESS);
		break;
	case 2: return x;
		break;
	default: std::cout << "Please only enter numbers 1-2 only. ";
		exitP(x);
		break;
	}

}
int main()
{
	int number = rand() %10;
	int guess = 0;
	int numcount = 1;
	int exitProgram = 1;
	guess = numcount ;
	numcount = numcount + 1;

	while (guess >= 0 && exitProgram == 1)
	{
		
		std::cout << "try to guess the random number :";
			std::cin >> guess;


		if (guess > number)
		{
			std::cout << "You, guessed your number to high, try again\n";
			std::cout << numcount;
		}
		if (guess < number)
		{
			std::cout << "You, guessed your number too low, try again\n";
			std::cout << numcount;
		}   
		if (guess == number)
		{
			std::cout << "You, guessed the correct number, What an amazing guess!\n";
			std::cout << "your tries:";
			std::cout << numcount;
			exitP(exitProgram);
		}
	}
}

Last edited on Jan 30, 2019 at 10:49pm
Jan 30, 2019 at 10:44pm
Thank you for using code formatting tags, but what is your question?

Always compile will warnings enabled
 In function 'int main()':
28:6: warning: unused variable 'entry' [-Wunused-variable]
 In function 'int exitP(int)':
23:1: warning: control reaches end of non-void function [-Wreturn-type]
 

https://www.learncpp.com/cpp-tutorial/configuring-your-compiler-warning-and-error-levels/
Last edited on Jan 30, 2019 at 10:45pm
Jan 30, 2019 at 10:48pm
I am having issues tracking my guesses with the numcount and the random number is not random.
Last edited on Jan 30, 2019 at 10:55pm
Jan 30, 2019 at 11:02pm
You need to seed your random number generator to get different numbers. Make a call to srand(static_cast<unsigned int>(time(0))) before calling rand(). To keep track of the number of guesses, simply increment the numcount by one after the user makes a guess.
Last edited on Jan 30, 2019 at 11:03pm
Jan 30, 2019 at 11:05pm
like this?

1
2
	srand(static_cast<unsigned int>(time(0))
	int number = rand() %10;
Jan 30, 2019 at 11:12pm
Yes, like that.

Okay, let's simplify your logic a bit, and also use a do-while loop instead of just a while loop, to prevent your logic from checking the guess before the user even guesses.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

bool exitPrompt()
{
	 // fill this in yourself,
	// have it return true if the user enters 2, false if 1.
	return true;
}

int main()
{
	srand((unsigned int)time(nullptr)); // initialize rand() based on current time
	int number = rand() % 10;
	int num_tries = 0;

	do 
	{
		num_tries++;
        
		std::cout << "Try #" <<  num_tries << ": try to guess the random number: ";
		int guess;
		std::cin >> guess;

		if (guess > number)
		{
			std::cout << "You, guessed your number to high, try again\n";
		}
		if (guess < number)
		{
			std::cout << "You, guessed your number too low, try again\n";
		}   
		if (guess == number)
		{
			std::cout << "You, guessed the correct number, What an amazing guess!\n";
			std::cout << "your tries: " << num_tries << '\n';
			if (exitPrompt())
				break;
		}

	} while (true);
}
Last edited on Jan 30, 2019 at 11:15pm
Topic archived. No new replies allowed.