1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <stdlib.h>
void mainmenu(std::string);
void start(std::string);
void check(std::string&);
void convert(std::string, unsigned int&);
void rand(unsigned int, unsigned int, unsigned int&);
void guessed(std::string, unsigned int, char&, unsigned int&, unsigned int&);
void quit(std::string);
int main()
{
std::string name;
do {
std::cout << "What is your name? " << std::flush;
std::getline(std::cin,name);
for(unsigned int i = 0; i<name.size(); i++) if(!isalpha(name[i])) name = "false";
if(name == "false") std::cout << "Invalid name.\n" << std::flush;
} while(name == "false");
std::cout << "I'm glad you could play my game " << name << '\n' << std::flush;
mainmenu(name);
return(0);
}
void mainmenu(std::string a)
{
std::string input;
do {
std::cout << a << " please enter \"P\" to play.\n" << a << " please enter \"Q\" to quit.\nChoice: " << std::flush;
std::getline(std::cin,input);
for(unsigned int i = 0; i<input.size(); i++) input[i] = toupper(input[i]);
if(input != "P" && input != "Q") std::cout << "Invalid choice " << a << ".\n" << std::flush;
} while(input != "P" && input != "Q");
switch(input[0]){
case 'P': start(a); break;
case 'Q': quit(a); break;
}
}
void start(std::string a)
{
std::string low, high, guess;
unsigned int l, h, r, i = 0;
char g;
do {
std::cout << a << " what range would you like?(ex. 1-100)...\nLowest Value: " << std::flush;
std::getline(std::cin,low);
std::cout << "Highest Value: " << std::flush;
std::getline(std::cin,high);
check(low);
check(high);
convert(low, l);
convert(high, h);
if(low == "false" || high == "false" || l > h) std::cout << "Invalid range " << a << ".\n" << std::flush;
} while(low == "false" || high == "false" || l > h);
rand(l, h, r);
do {
i++;
std::cout << a <<" please guess a number between " << l << " and " << h << "\nGuess " << i << ": " << std::flush;
std::getline(std::cin,guess);
check(guess);
if(guess != "false") guessed(guess, r, g, l, h);
else std::cout << "Invalid guess " << a << '\n' << std::flush;
} while (g != 't');
if(i != 1) std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " tries to guess the random number.\n\n" << std::flush;
else std::cout << "\nGreat job " << a << "!\nIt only took you " << i << " try to guess the random number.\n\n" << std::flush;
mainmenu(a);
}
void check(std::string &a)
{
for(unsigned int i = 0; i<a.size(); i++) if(!isdigit(a[i])) a = "false";
}
void convert(std::string a, unsigned int &b)
{
b = atoi(a.c_str());
}
void rand(unsigned int a, unsigned int b, unsigned int &c)
{
srand(time(0));
c = rand() % (b - a + 1) + a;
}
void guessed(std::string a, unsigned int b, char &c, unsigned int &d, unsigned int &e)
{
unsigned int guess;
convert(a, guess);
if(guess < d || guess > e) std::cout << "Invalid guess.\n" << std::flush;
if(guess > b && guess < e){ std::cout << "You guessed too high.\n" << std::flush; e = guess - 1; }
if(guess < b && guess > d){ std::cout << "You guessed too low.\n" << std::flush; d = guess + 1; }
if(guess == b) c = 't';
}
void quit(std::string a)
{
std::cout << "Good bye " << a << ".\nPlease play again!" << std::flush;
}
|