Writing a random number generator

I have been working on this random number generator for a few days now. The problem that I have with it is when I run the program and go to enter the number I am guessing, the program will just end. It does not run through any of the other code I have wrote. It does not display any of the messages I have wrote into the code. I am not sure where exactly I went wrong with it. Any help is very appreciated.


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

// declare variables
int number, guess, answer;


int main()

{
	// get the system time
	unsigned seed = time(0);

	// seed the random number generator
	srand(seed);


	do {
		// generate random number between 1 and 100
		number = (rand() % (100 - 1 + 1)) + 1;

		// ask the user to guess the randomly generated number
		cout << "Guess a number between 1 and 100:" << endl;
		cin >> guess;

		for (int count = 0; count < 10; count++)
		{

			// validate the users input
			if ((guess < 1) || (guess > 100))
			{
			cout << "You must guess a number between 1 and 100. Guess again" << endl;
			count--;
			}

			// the user has less than 5 guesses
			else if ((guess == number) && (count < 5))
			{
				cout << "Either you know the secret or you got lucky!" << endl;
				break;
			}
			// user has between 5 and 7 guesses
			else if ((guess == number) && (count >= 5) && (count <= 7))
			{
				cout << "You're pretty good at this!" << endl;
				break;
			}
			// user has between 8 and 10 guess
			else if ((guess == number) && (count >= 8) && (count <= 10))
			{
				cout << "You'll do better next time.";
				break;
			}
			// user has guessed more than 10 times
			else if (count > 10)
			{
				cout << "Sorry. You have taken too many guesses." << endl;
				break;
			}

			// user has guessed above 100
			else if (guess > 100)
			{
				cout << "Too high, try again." << endl;
				cin >> guess;
				break;
			}
			// user has guessed less than 1
			else if (guess < 1)
			{
				cout << "Too low, try again." << endl;
				cin >> guess;
				break;
			}
		}
		
	} while ();

	return 0;

}
Last edited on
Hello Tipper1997,

Welcome to the forum.

Some things I see right off:

The include for "pch.h" should be the last include statement not the first. And since you did not include this header file with your code I have no idea what is there or how it acts with the program.

When you write a line of code that requires {}s be consistent with how you use them. I suggest using the form the you used with main as this has more advantages the what you did with the do/while loop.

Speaking of the while condition it should be written as } while (1); to work as an endless loop. I am not sute what empty ()s will do.

The last two "else if" statements deal with a guess being above 100 or less than one. You already dealt with this in the "if" statement, but failed to get a new guess.

The last two "else if" state I believe would work better to check "guess" against 'number" to see if the guess is higher or lower than the number.

The do/while loop means that the code will execute one time before it reaches the while condition, so maybe you are reaching a "break" ststement before you need to.

My computer is a bit slow these days, so it will be a little while before I can get your program loaded up and tested.

Hope that helps,

ANdy
Thank you,
I will take your advice and see if I can get my program running.
I should let you know though the the #include "pch.h" is something that my computer requires to run the program. I'm not sure what it means, my professor is also not sure what it means. In fact he said that he hadn't seen it before this semester and several other people have used it as well. It is preloaded into the C++ form upon opening and will not run without it.
Use code formatting, represented by the <> symbol. Syntax is (code) code here (/code)
with square brackets instead of parentheses.
Sorry Syvel, i don't quite understand what you mean
[code] code here [/code]
with
//code goes here
std::cout<<"wow";
code

would make

1
2
//code goes here
std::cout<<"wow";
Last edited on
Hello Tipper1997,

Forgot to mention this last time>

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Since I put all the comments in the program, I will post this to help you along:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>


//using namespace std;  // <--- Best not to use.


int main()
{  // <--- Best way to use {}s.
	// declare variables
	int number, guess, answer;

	// get the system time
	unsigned seed = time(0);

	// seed the random number generator
	srand(seed);


	do
	{  // <--- Channged.
		// generate random number between 1 and 100
		number = (rand() % (100 - 1 + 1)) + 1; // <--- All you need is rand() % 100 + 1;

		//std::cout << "\n The computer's number is: " << number << '\n';  // <--- Used for testing.

		// ask the user to guess the randomly generated number
		std::cout << "\nGuess a number between 1 and 100 ( -9 to quit): "; // <--- Notice the "\n". This makes the output more readable.
		std::cin >> guess;

		if (guess = -9)  // <--- To end the program.
			break;  // <--- Breaks out of the do/while loop.

		for (int count = 0; count < 10; count++)
		{

			// validate the users input
			if ((guess < 1) || (guess > 100))
			{
				// <--- The extra spaces after the "\n" are there to make this line different from the rest.
				std::cout << "\n    You must guess a number between 1 and 100. Guess again: ";
				count--;
				std::cin >> guess;
			}

			// <--- In all my testing these next line appear to work.
			// the user has less than 5 guesses
			else if ((guess == number) && (count < 5))
			{
				std::cout << "\nEither you know the secret or you got lucky!" << std::endl;
				break;
			}
			// user has between 5 and 7 guesses
			else if ((guess == number) && (count >= 5) && (count <= 7))
			{
				std::cout << "\nYou're pretty good at this!" << std::endl;
				break;
			}
			// user has between 8 and 10 guess
			else if ((guess == number) && (count >= 8) && (count <= 10))
			{
				std::cout << "\nYou'll do better next time.";
				break;
			}
			// user has guessed more than 10 times
			else if (count > 10)
			{
				std::cout << "\nSorry. You have taken too many guesses." << std::endl;
				break;
			}

			// <--- This should be "user had guessed above the computer number. Not what your comment says.
			// user has guessed above 100
			else if (guess > number)
			{
				std::cout << "Too high, try again.: ";
				std::cin >> guess;
				//break;
			}
			// <--- Same idea as above.
			// user has guessed less than 1
			else if (guess < number)
			{
				std::cout << "Too low,  try again.: ";
				std::cin >> guess;
				//break;
			}
		}

	} while (1);  // <--- The while needs a condition. 1 or "true" creates an endless loop.

	// <--- These next lines I needed. Use them if you want or delete is you do not need.
	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue";
	std::cin.get();

	return 0;
}

Your "pch.h file sounds like the "stdafx.h" file needed in Visual Studio (VS). Eventually I figured out how to create an empty project where I did not need the "stdafx.h" file. You should have the same ability. In the mean time start your highlight one line lower. This will avoid the comments and questions about the "pch.h" file.

In the program noticed how I used the "\n"s to break up the output so that is is not all run together and becomes more readable.

I also added some code to break out of the do/while loop to end the program.

Hope this helps,

Andy
Topic archived. No new replies allowed.