Help❓ A Simple C++ RNG Program (Novice)

Posts 1–3 of 3 · Page 1 of 1
❓ A Simple C++ RNG Program (Novice)
It has come to my last resort of posting on a forum thread for aid in my journey through C++ programming. I have been running through a book for game programming with C++/Visual C++. One of the examples the book had me write was to create a basic 'Guess My Number' game where I guess what the computer's number is. I successfully finished that and I am on to one of the exercises at the end of the chapter where I am required to rewrite that code into a situation where I pick a number and through a series of loops, get the computer to guess my number. I have a basic understanding of loops and how they work; however, that being said, I do not know the full extent to the power and the control of loops. The code I have pasted here from pastebin is a concoction of what I am attempting to resemble that. When compiled, it will ask the first question of what I want the number to be and will continue to ask if it is higher or lower than the number I chose. After completing those functions, it will spiral into an infinite loop spawned from my do/while loop combination. I read to be weary using both in a situation as this but I felt it a useful tool for completing this. Perhaps I am missing something? I'd love some insight as I've spent 8.5 hours attempting to debug this. Perhaps I've incorrectly called assigned certain variables to my functions and it merely ignores the fact I'm trying to repeat the block until my number is achieved. I've also noticed after the first random number is displayed from the computer, it will repeatedly go from 0 to that random number in every attempt thereafter. Do I need another seed for my RNG to work for the computer to pull another number up or multiple instances of my computerGuess variable? Much appreciated for any input.

And no I do not want to look up the solution from the book as I won't learn as much from seeing the solution than I will from community input.

pastebin(dot)com/2e4Kzm4g [I am not currently allowed to post links so I thought I could do this to combat that; replace (dot) with a '.']
 
Code For Ease of Access

// Guess my Number
// The classic number guessing game

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;


int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int computerGuess = rand() % 100 + 1; // random number betweeen 1 and 100
int myNumber = 1;
int tries = 0;
int high = (computerGuess > myNumber);
int low = (computerGuess < myNumber);
int numberPosition;

cout << "\tWelcome to Guess My Number\n\n";
cout << "\n Type a number for the computer to guess: ";
cin >> myNumber;

while (computerGuess != myNumber)
{

cout << "Computer first guess: " << computerGuess;
++tries;

if (computerGuess > myNumber)
{
cout << "\nIs that number too high or too low?: ";
cin >> numberPosition;
if (high)
{
cout << "I'll guess again: " << (computerGuess < myNumber);
++tries;
cout << "\nIs that number too high or too low?: ";
cin >> numberPosition;



}
else if (low)
{
cout << "I'll guess again: " << (computerGuess > myNumber);
++tries;
cout << "\nIs that number too high or too low?: ";
cin >> numberPosition;

}

else if(computerGuess == myNumber)
{
cout << "\nI got it didn't I? And I only did it in " << tries << " guesses!\n";

}
}
}

return 0;
}


Also, I do know I am but a mere novice in C++ and could have overlooked any simple mistake. Treat this as if you're talking to an annoying toddler whom doesn't know the difference between a spoon and fork.
 
Spoiler

Code:
// Guess my Number
// The classic number guessing game

#include <iostream> 
#include <cstdlib> 
#include <ctime>

using namespace std;

int main() 
{
  srand(static_cast < unsigned int > (time(0))); //seed random number generator
  int minGuess = 1;
  int maxGuess = 100;
  int computerGuess = -1; // initialize to -1 for now since we havent guessed yet
  int myNumber = 1;
  int tries = 0;

  cout << "\tWelcome to Guess My Number\n\n";
  cout << "\n Type a number for the computer to guess: ";
  cin >> myNumber;

  while (computerGuess != myNumber) 
  {
	computerGuess = rand() % (maxGuess-minGuess+1) + minGuess; // Have a guess between min and max.
    cout << "Computer guess: " << computerGuess << std::endl;
    ++tries;

    if (computerGuess > myNumber) {
      cout<<"It's too high? Hmm... Let's try that again!"<<std::endl; 
	  maxGuess = computerGuess-1; // lower maxGuess to our last guess - 1
    }
	else if (computerGuess < myNumber) {
      cout<<"Whaaaat? Too low??? Hmmm... Let's try that again!"<<std::endl; 
	  minGuess = computerGuess+1; // increase minGuess to our last guess + 1
    }
	else if (computerGuess == myNumber) {
		cout << "\nI got it didn't I? And I only did it in " << tries << " guesses!\n";
	}
  }

  return 0;
}


Compare this to your version, see if you can figure out where you went wrong.
Ahhh, yes. I see you adjusted the computer's guess with a minGuess and a maxGuess according to if the first guess was too high or too low and changing the declaration of each of those variables themselves. Much better than having a static window of numbers that does not change. Also, you put the rand() within the block itself and I understand that is much better than leaving it outside so it could be easily visible to what it is applied to and such. I was perhaps trying to make this a tad bit too complicated. Although, I appreciate your comparable program to troubleshoot my problem. Cheers.
Posts 1–3 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Talk with us