merc
By: a guest | Feb 9th, 2010 | Syntax:
C++ | Size: 1.84 KB | Hits: 25 | Expires: Never
//number guess
#include "stdafx.h" //visual studio seems to require this
#include <ctime> //needed for srand()
#include <cstdlib> //needed for rand()
#include <iostream> //needed for cout/cin?
#include <string> // needed to use string class
using namespace std; // stops me typing std:: before cout and cin
int _tmain(int argc, _TCHAR* argv[])
{
string name;
bool tryagain = true;
char tryagaintext;
cout << "What's yer name dude\? ";// BTTF reference
getline (cin, name); // to stop cin from not reading past a space
if (name == "Clint Eastwood") // BTTF reference
{
cout << "What kind of stupid name is that\?\!\?\n";
}
while (tryagain == true) //keep asking for number iuntil they get it right
{
int theirnum;
int tries = 0;
srand((unsigned)time(0)); //generates random number based on time as rand()
int mynum = rand(); // make number to guess random
while (mynum > 100)// while loop to ensure number is always below 100
{
mynum = mynum/4; // if not, divide by four until it is..
}
cout << "Guess the number(1 - 100): ";
cin >> theirnum;
while (theirnum != mynum)
{
if (theirnum > mynum)
{
cout << "The number is smaller :(\n";
tries++;
}
else if (theirnum < mynum)
{
cout << "The number is bigger :D\n";
tries++;
}
cout << "Guess again: ";
cin >> theirnum;
}
cout << name <<" got it right in " << tries << " tries\n";
tries = 0;
cout << "Try again? \(Y\\N\) ";
cin >> tryagaintext;
if (tryagaintext == 'N' | tryagaintext == 'n')//do they want to play again?
{
tryagain = false;
}
else if (tryagaintext == 'Y' | tryagaintext == 'y')
{
tryagain = true;
}
else
{
tryagain = false;
}
} // end while
//pause program
system("pause");
// terminate the program:
return 0;
}