Simple random number guessing game

-
Last edited on
Hello tomato1904,

After looking over everything this is what I would likely do,

At the beginning of the program:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

constexpr size_t MAX_NUM_OF_TRIES{ 5 }; // <--- Used for testing. Change to 1000 when finished.

/** */

Line 8 will make writing the code easier and easier to maintain.

When you define "min" and "max"in "main" I would initialize the variables. In this case it works, but very easy to be a problem in other programs. From C++11 on you can write: int min{}, max{};. The empty {}, (uniform initializer), will set the value to (0) zero or you can put a number between the {}s.

In the function "getNumberRange" some minor things: In the first "cout" statement I would make a note there to use only positive numbers. And maybe that the difference between "min" and "max" should be at least a certain amount like 25 or 50. This way you could avoid having "min" be 1 and "max" be 2.

Reading the directions

Hint: A recursive call to getNumberRange() for invalid ranges is easier than using a loop to implement.


The return value of the function would be useful as a recursive function, but for what you have now a "void" function would work best.

For now that is as far as I have progressed since you need to get that function working correctly before you precede to what is next.

Andy
Topic archived. No new replies allowed.