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)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace std;
constint high = 100;
constint 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<unsignedint>(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();
}
elseif (response == 1)
{
cout << "\n\nToo high eh? I'll take another guess.\n\n";
highGuess = guess; //altering the upper limit of random number range
}
elseif (response == 2)
{
cout << "\n\nToo low eh? I'll take another guess.\n\n";
lowGuess = guess; //alteing the lower limit of random number range
}
elseif (response == 3)
{
correct = 1;
}
return 0;
}
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.