Random Number Generator?

This is a discussion on Random Number Generator? within the C++ Programming forums, part of the General Programming Boards category; You need both ignore() (to eat the newline the user entered) and get() (to keep your console window from closing). ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You need both ignore() (to eat the newline the user entered) and get() (to keep your console window from closing).
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ( int argc , char** argv ) 
    
    { 
        srand(time(0));
        int random_number = rand() % 10 + 1 ;
        cout << "Guess a number between 1 and 10\n";
        int guess;
        cin >> guess;
        if (guess==random_number)
        {
            cout << "Correct!";
        }
        else if (guess!=random_number)
        {
            cout << "Wrong!";
        }
        cin.ignore(80,'\n');
        cin.get();
    }

  2. #17
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Ok, so now that works, but I seem to be having problems with my compiler . Whenever I compile, it should create a new .exe for my program, but now it doesn't create it (it used to). I am using Dev C++. Can anyone help me on that?

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Ok, so now that works,
    How can it work and not create an .exe? If:
    "Guess a number between 1 and 10"
    pops up, then there's an exe somewhere.

  4. #19
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88
    Quote Originally Posted by DZeek
    I tried cin.get(); but it still closes after you guess a number (cin >> guess; ).
    Before the window closes, I can see it show "Correct" or "Wrong", but it doesn't ask for input again. It just closes.
    ok , ( tell me if I'm wrong ) I think it gets affected by the other cin in your program : it kinda skips it. what you can do is put two cinget(); after eachother... ok its not the best code but I'm sure it works ( or cin.get(); cin.ignore(); )
    PLay MystWind beta , within two years

  5. #20
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Quote Originally Posted by swoopy
    >Ok, so now that works,
    How can it work and not create an .exe? If:
    "Guess a number between 1 and 10"
    pops up, then there's an exe somewhere.
    It works, but I just can't seem to find the exe. All the other programs I compiled had the exe in the same folder as the script, but this one doesn't...

  6. #21
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by DZeek
    It works, but I just can't seem to find the exe. All the other programs I compiled had the exe in the same folder as the script, but this one doesn't...
    Usually your program will be located in a "Debug" or "Release" folder, which can be found inside the same folder as your project file.

  7. #22
    Hack the Planet!
    Join Date
    Mar 2005
    Posts
    23
    Problem solved. I saved it as "guess1.0" and the period messed it up. I resaved it without the period, and it creates the exe.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  2. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. how to link random number generator with cpu?
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2003, 05:26 AM
  5. Seeding Random Number Generator
    By zdude in forum C++ Programming
    Replies: 2
    Last Post: 09-05-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21