-
number guessing game
Code:
#include <iostream>
#include <time.h>
using namespace std;
int rnd (void)
{
return (rand() % 100 + 1); // returns a number between 1 and 100
}
void main (void)
{
srand (time(NULL)); // to seed the random number generator
int guess;
int choice;
char input;
bool playagain;
int number;
playagain = false;
while (!playagain)
{
cout << "Welcome to my number guessing game! " << endl;
cout << "Which game do you want to play ?" << endl;
cout << " User to guess a number generated randomly by the program " << endl;
cout << "Select number '1' for this game" << endl;
cout << " User think up a number for the program to guess " << endl;
cout << "Select number 2 for this game" << endl;
cout << "Select your choice: " ;
cin >> choice;
while (choice < 1 || choice > 2)
{
cout << "Please enter '1' or '2' of the number only" << endl;
cout << "Enter again: " ;
cin >> choice;
}
if (choice == 1)
{
bool isGuessed;
int num = rnd();
isGuessed = false;
cout << "You had selected the first game" << endl;
cout << "Welcome to my first part of the game" << endl;
while (!isGuessed)
{
cout << "Enter an integer greater or equal to 0 and less than 100: ";
cin >> guess;
while (guess < 1 || guess > 100)
{
cout << "Your number had been out of range, please enter number between 1 to 100 only." << endl;
cout << "Enter again: " ;
cin >> guess;
}
cout << endl;
if (guess == num)
{
cout << "You guessed the correct number" << endl;
cout << "My random number is " << num << endl;
cout << "Thank you for playing my number guessing game." << endl;
isGuessed = true;
}
else if (guess < num)
{
cout << "Your number is smaller.Guess again";
cout << endl;
}
else
{
cout << "Your number is higher. Guess again";
cout << endl;
}
}
}
else if (choice == 2)
{
bool iscorrect = false;
cout << "You had choose for the program to guess the number for you to" << endl;
cout << "input it" << endl;
cout << "Welcome to my second part of my game. " << endl;
while (!iscorrect)
{
int num = rnd();
cout << num << endl;
cout << "Is this your number, L stand for too low, H stand for too high and " << endl;
cout << "C stand for correct" << endl;
cin >> input;
cout << endl;
if (input == 'C' || input == 'c')
{
cout << "The program has guess your number correctly" << endl;
cout << "This is your number I had guess correctly : " << num << endl;
cout << "Thank you for playing my number guessing game." << endl;
iscorrect = true;
}
else if (input == 'H' || input == 'h')
{
cout << "the program guess too high" << endl;
}
else if (input == 'L' || input == 'l')
{
cout << "the program guess too loww" << endl;
}
else
{
cout << "Invalid input" << endl;
}
}
}
cout << "Do you want to play again. " << endl;
cout << "Press 1 to play agin or press 2 to stop the game." << endl;
cin >> number;
while (number < 1 || number > 2)
{
cout << "Press only 1 or 2. " << endl;
cout << "Press a number again." << endl;
cin >> number;
}
if (number == 1)
{
cout << "Lets play again. " << endl;
}
else
{
cout << "Thank you for playing" << endl;
playagain = true;
}
}
system ("pause");
}
Hello, i am back again. I need help. I got a problem. For my part 2 game where the program would print a random number and the user are to tell the computer that if the number is too high or too low or the random number is correct. But, if the user said number is too low, but still the random number would still give a lower number . Is there any techniques to code such that if the user said the number is low, the computer would random a higher number.
-
Cprogramming.com FAQ > Generate random numbers?
For getting a random number between two limits.
The idea being, if the user inputs 'H', then the number you guessed is your new upper bound.
Also, consider this as a way of improving your code structure (so it's not all in main).
Code:
if (choice == 1) {
playHumanGuesses();
} else
if (choice == 2) {
playComputerGuesses();
}
Move appropriate blocks of code into new functions.
-
Class Based
Per Salem's tip here is some class based code using your original code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using namespace std;
int randNum()
{
return ((rand() % 100) + 1);
}
class numGame
{
public:
numGame() {};
void progGen();
void userGen();
};
void numGame::userGen()
{
bool iscorrect = false;
char input;
cout << "You had choose for the program to guess the number for you to" << endl;
cout << "input it" << endl;
cout << "Welcome to my second part of my game. " << endl;
while (!iscorrect)
{
int num = randNum();
cout << num << endl;
cout << "Is this your number, L stand for too low, H stand for too high and " << endl;
cout << "C stand for correct" << endl;
cin >> input;
cout << endl;
if (input == 'C' || input == 'c')
{
cout << "The program has guess your number correctly" << endl;
cout << "This is your number I had guess correctly : " << num << endl;
cout << "Thank you for playing my number guessing game." << endl;
iscorrect = true;
}
else if (input == 'H' || input == 'h')
{
cout << "the program guess too high" << endl;
}
else if (input == 'L' || input == 'l')
{
cout << "the program guess too loww" << endl;
}
else
{
cout << "Invalid input" << endl;
}
}
}
void numGame::progGen()
{
bool isGuessed=true;
int num = randNum();
int guess;
cout << "You have selected the first game." << endl;
cout << "Welcome to my first part of the game" << endl;
while (isGuessed)
{
cout << "Enter an integer greater or equal to 0 and less than 100: ";
cin >> guess;
while (guess < 1 || guess > 100)
{
cout << "Your number had been out of range, please enter number between 1 to 100 only." << endl;
cout << "Enter again: " ;
cin >> guess;
}
cout << endl;
if (guess == num)
{
cout << "You guessed the correct number" << endl;
cout << "My random number is " << num << endl;
cout << "Thank you for playing my number guessing game." << endl;
isGuessed = false;
}
else if (guess < num)
{
cout << "Your number is smaller.Guess again";
cout << endl;
}
else
{
cout << "Your number is higher. Guess again";
cout << endl;
}
}
}
int main ()
{
srand(time(NULL));
numGame ng;
int choice;
bool playagain=true;
char playG;
try
{
while (playagain)
{
cout << "Welcome to my number guessing game! " << endl;
cout << "Which game do you want to play ?" << endl;
cout << " User to guess a number generated randomly by the program " << endl;
cout << "Select number '1' for this game" << endl;
cout << " User think up a number for the program to guess " << endl;
cout << "Select number 2 for this game" << endl;
cout << "Select your choice: " ;
cin >> choice;
while (choice < 1 || choice > 2)
{
cout << "Please enter '1' or '2' of the number only" << endl;
cout << "Enter again: " ;
cin >> choice;
}
if (choice == 1)
{
ng.progGen();
}
else if (choice == 2)
{
ng.userGen();
}
bool guessPlay=true;
while (guessPlay == true)
{
cout << endl << "Do you want to play again? ";
cin >> playG;
if ((playG == 'Y') || (playG == 'y'))
{
playagain = true;
guessPlay = false;
}
else if ((playG == 'N') || (playG == 'n'))
{
playagain = false;
guessPlay=false;
}
else
{
cout << endl << "Enter Y or N" << endl;
}
}
}
}
catch (exception& e)
{
cout << "Exception thrown " << e.what() << endl;
return 1;
}
return 0;
}
I tested it a few times, but might still be some things I missed.
-
Why is it that no one complained about void main?
SourceForge.net: Void main - cpwiki
-
Well
I changed it to int on rewrite.