Thread: help with rand

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    Question help with rand

    Hey, I'm back. YOu were right, Java is good for a while, but i ahd to come back to C++. anyway, I've tried to write a program that chooses a number, and the user has to guess it. I also want it to display the number of guess when the user is correct. I kind of forget what im doing, but this is what I have:

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        char ans;
        
        do {
            int fav = std::rand() % 10;
            int num;
            int guess = 1;
            do { 
                 std::cout << "Pick a number from 1 to 10\n";
                 std::cin >> num;
                  if(num != fav){ ++guess;
                  }   
                  std::cout << (num < fav ? "Low" :
                               num > fav ? "High"  :
                                   "You are Correct!\a\n It took you"
                                         std::cout << guess<< "Guesses")
                     
                          << std::endl;
                          }while (num != fav);
                          std::cout << "Try again? y/n";
                          std::cin >> ans;
                          }while (ans == 'y');
                 return 0;
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    I think you want to use srand as well to seed the random number generator.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int fav = std::rand() % 10;
    And if you want a number from 1 to 10, then make this:
    int fav = std::rand() % 10 + 1;

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you have the line
    using namespace std;
    in your code, then you don't need to preface cin and cout with std::

    Don't you the ternary operator for nested ifs. It makes your code incredibly unreadable.

  5. #5
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I also recommend you pair up your braces
    Code:
    {
         blah;
    }
    The code is a lot easier to read if the opening and closing braces of the same scope are lined up with each other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Wm_timer
    By Ducky in forum Windows Programming
    Replies: 21
    Last Post: 09-26-2008, 05:36 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM
  5. rand () a little confusion
    By Led Zeppelin in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 10:13 PM