Guest
Public paste!

merc

By: a guest | Feb 9th, 2010 | Syntax: C++ | Size: 1.84 KB | Hits: 25 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. //number guess
  2. #include "stdafx.h" //visual studio seems to require this
  3. #include <ctime> //needed for srand()
  4. #include <cstdlib> //needed for rand()
  5. #include <iostream> //needed for cout/cin?
  6. #include <string> // needed to use string class
  7. using namespace std; // stops me typing std:: before cout and cin
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10.  
  11.         {
  12.         string name;
  13.         bool tryagain = true;
  14.         char tryagaintext;
  15.  
  16.         cout << "What's yer name dude\? ";// BTTF reference
  17.         getline (cin, name); // to stop cin from not reading past a space
  18.         if (name == "Clint Eastwood") // BTTF reference
  19.                 {
  20.                 cout << "What kind of stupid name is that\?\!\?\n";
  21.                 }
  22.         while (tryagain == true) //keep asking for number iuntil they get it right
  23.                 {
  24.                 int theirnum;
  25.                 int tries = 0;
  26.                 srand((unsigned)time(0)); //generates random number based on time as rand()
  27.  
  28.                 int mynum = rand(); // make number to guess random
  29.                 while (mynum > 100)// while loop to ensure number is always below 100
  30.                 {
  31.                         mynum = mynum/4; // if not, divide by four until it is..
  32.                 }
  33.                 cout << "Guess the number(1 - 100): ";
  34.                 cin >> theirnum;
  35.                 while (theirnum != mynum)
  36.                         {
  37.                         if (theirnum > mynum)
  38.                                 {
  39.                                 cout << "The number is smaller :(\n";
  40.                                 tries++;
  41.                                 }
  42.                         else if (theirnum < mynum)
  43.                                 {
  44.                                 cout << "The number is bigger :D\n";
  45.                                 tries++;
  46.                                 }
  47.                         cout << "Guess again: ";
  48.                         cin >> theirnum;
  49.                         }
  50.                 cout << name <<" got it right in " << tries << " tries\n";
  51.                 tries = 0;
  52.                 cout << "Try again? \(Y\\N\) ";
  53.                 cin >> tryagaintext;
  54.                 if (tryagaintext == 'N' | tryagaintext == 'n')//do they want to play again?
  55.                         {
  56.                         tryagain = false;
  57.                         }
  58.                 else if (tryagaintext == 'Y' | tryagaintext == 'y')
  59.                         {
  60.                         tryagain = true;
  61.                         }
  62.                 else
  63.                         {
  64.                         tryagain = false;
  65.                         }
  66.                 } // end while
  67.         //pause program
  68.         system("pause");
  69.         // terminate the program:
  70.         return 0;
  71.         }