❓ 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 '.']
// 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.
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.