while we're on the subject...any idea why this won't work for all ranges?
//Brian W. Papocchia
//Programming Assignment Set 3
//Part 4, Guessing Game program
//May 29, 2003#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;void Instructions(void); //game instructions
void GetRange(int &, int &); //ask user for range to guess within
void GetSeed(void); //set the random number generator seed
int GetRandom(int &, int &); //set a random integer in range
void PlayGame(int &, int &); //game routine
void YouWin(int &, int &, int); //user guesses number
void YouLose(int &); //user fails to guess numbervoid main(void)
{
int from, to; //range to guess withinInstructions();
GetRange(from, to);
while (from>0 || to>0)
{
PlayGame(from, to);
cout << "If you want to play again..." << endl;
GetRange(from, to);
cout << endl;
}
cout << "Game over." << endl;
}void Instructions(void) //game instructions
{
cout << "This program is a Guessing Game." << endl;
cout << "You will have 10 guesses at my number." << endl;
cout << "I will tell you if your guess is too high or too low." << endl;
cout << "You can even define the range for my number (ex. 1 to 100)." << endl;
cout << "---------------------------------------------------------------" << endl;
}void GetRange(int &from, int &to) //set range of number to guess within
{
cout << "Please enter a range from which to guess." << endl;
cout << "Enter two zeros or negative numbers to stop the program." << endl;
cout << "---------------------------------------------------------------" << endl;
cout << "Enter the low end of the range: ";
cin >> from;
cout << "Enter the high end of the range: ";
cin >> to;
cout << "---------------------------------------------------------------" << endl;
return;
}void GetSeed(void) //set the random number generator seed
{
srand(unsigned(time((time_t *)NULL))); /*sets seed based on computer time
thus seed will be different on each execution of program*/
}int GetRandom(int &low, int &high) //set a random integer in range
{
double random;
random = rand() / ((double)(RAND_MAX) + 1);
return (int)((high * random) + low);
}void PlayGame(int &from, int &to)
{
const int maxguesses = 10; //set maximum allowed guesses
int count; //count guesses
int guess; //store user's guess
int number; //computer's number to guessGetSeed();
number = GetRandom(from, to);
count = 1;
while (count <= maxguesses)
{
cout << "What is your guess? ";
cin >> guess;
if (guess == number)
{
YouWin(number, count, maxguesses);
break; //break out of while loop
}
else if (guess > number)
cout << "Your guess is too high." << endl;
else cout << "Your guess is too low." << endl;
cout << endl;
count++;
}
if (count > maxguesses)
YouLose(number);
return;
}void YouWin(int &num, int &guesses, int allowed)
{
cout << "---------------------------------------------------------------" << endl;
cout << "VERY GOOD!!!" << endl;
cout << endl;
cout << "My number was "<< setw(4) << num << ", and you got it in " << guesses;
if (guesses == 1)
cout << " try! Did you cheat? *wink*";
else cout << " tries.";
cout << endl;
if (guesses == allowed)
cout << "WHEW!!! YOU JUST MADE IT!!!" << endl;
cout << "---------------------------------------------------------------" << endl;
cout << endl;
return;
}void YouLose(int &num)
{
cout << "I'm sorry. You are out of guesses." << endl;
cout << "My number was: " << num << "." << endl;
cout << "---------------------------------------------------------------" << endl;
cout << endl;
return;
}
works good for 1 to 100, or even 1 to 3. But, pick 44 to 46, and the number ends up as 68. weird...i'll have to look at it some more tomorrow.
I made a game similiar to that in VB. I also did a tic tac toe game in C++ (if I can find it). stupid languages.....