Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Click Here
Getting C++ Help is Easy!

Join 117,572 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,982 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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: 5,150

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!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month