Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,252 people online right now. Registration is fast and FREE... Join Now!





Guessing Game

Using a random number, you have to guess the number.

Submitted By: DeeViLiSh
Actions:
Rating:
Views: 8,625

Language: C++

Last Modified: August 29, 2006

Snippet


  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main ()
  7. {
  8.     long secretnumber = 0, user_number = 0;
  9.     const long MAX = 100, MIN = 1;
  10.  
  11.     // Create random number
  12.     srand(time(NULL));
  13.     secretnumber = (rand() % (MAX - MIN + 1)) + MIN;
  14.  
  15.     cout<<"I'm going to randomize a number and you have to find it!"<<endl<<endl;
  16.    
  17.     // Loops forever until user finds the number
  18.     while (user_number != secretnumber)
  19.     {
  20.         cout<<"Pick a number : ";
  21.         cin >> user_number;
  22.  
  23.         if (secretnumber > user_number)
  24.             cout << "Higher!"<<endl<<endl;
  25.  
  26.         else if (secretnumber < user_number)
  27.             cout << "Lower!"<<endl<<endl;
  28.  
  29.         else
  30.             cout << "Good job! You found the number!"<<endl<<endl;
  31.  
  32.     }
  33.     system("PAUSE");
  34. }

Copy & Paste


Comments


FARROUKH 2008-05-08 23:57:48

instead of using srand we can also use randomize()function which can do automatic initialization of random number with time seeds.

FARROUKH 2008-05-09 00:01:22

like #include #include #include const Max = 100; const Min = 10; void main() { long secretnumber=0; long picknumber; randomize(); secretnumber=random(Max-Min+1)+ Min; cout


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month