Random number between range

Hi Guys,

I'm trying to make a guess my number program, with the computer guessing the number that I choose, I seem to have finally got it working except for the random number range, the high number works but the low number doesn't,

I guess I shouldn't be doing lowGuess=rand() but I have no idea what I should be doing instead, could somebody point me in the right direction please?

Also feel free to give me feedback on the rest of the code, this is my first attempt at writing something myself. (with a little reference material)

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;

const int high = 100;
const int low = 1;
int lowGuess=1;
int highGuess=100;
int myNumber=0;
int guess=0;
int guesses=0;
bool correct = 0;

int askNumber();
int askResponse();
int guessNumber();

int main()
{

	askNumber();

	do 
	{
		guessNumber();

		askResponse();
	} while (correct == 0);
	cout << "Yes!! I guesed your number in " << guesses << " guesses.";

	return 0;
}


int askNumber()
	{
	cout << "\n\nEnter a number between " << low << " - " << high << ".\n\n";
	cin >> myNumber;

	if (myNumber < low || myNumber >high)
		{
		return askNumber();
		}
	}

int guessNumber()
{
	srand(static_cast<unsigned int>(time(0)));
	lowGuess = rand();						//im doing something wrong here with lowGuess
	guess = (lowGuess % highGuess) + 1;		//im trying to generate a random number between
	cout << "\n\nMy guess is " << guess << endl;	//the value of lowGuess and highGuess
	guesses += 1;							//highGuess is working as intended but lowGuess isn't

	//printing values to see them working
	cout << "low " << lowGuess << " high "<<highGuess << endl;	

	return 0;
}

int askResponse()
{
	int response;
	cout << "\n\nIs my guess too high, too low, or correct?\n\n";
	cout << "1. Too High.\n";
	cout << "2. Too Low.\n";
	cout << "3. Correct.\n\n";

		cin >> response;
	if (response < 1 || response > 3)
	{
		return askResponse();
	}
	else if (response == 1)
	{
		cout << "\n\nToo high eh? I'll take another guess.\n\n";
		highGuess = guess;						//altering the upper limit of random number range
	}
	else if (response == 2)
	{
		cout << "\n\nToo low eh? I'll take another guess.\n\n";
		lowGuess = guess;						//alteing the lower limit of random number range
	}
	else if (response == 3)
	{
		correct = 1;
	}

	return 0;
	
}
Last edited on
I think you had better take a look at ALL your return statements: there are quite a few wrong ones.

If you return anything then it should be a value, not another call to the same function (e.g. lines 46, 75).

Actually, none of your functions need return anything (if you are using global variables like this) - you might as well make them void.
lastchance wrote:
I think you had better take a look at ALL your return statements: there are quite a few wrong ones.

There is nothing wrong with the OP's return statements. The OP is using recursion to repeat the question until valid input is received. Not a technique I like, but it works.
Topic archived. No new replies allowed.