Thread: Will not compile, help

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    11

    Will not compile, help

    This wont compile correctly, it says "implicit declaration of function `int random(...)'"

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <iostream.h>
    int main()
    {
    int topf;
    topf=random(5);
    cout<<topf;
    return 0;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    there is no function called random()
    Those headers are deprecated in c++ use c++ headers
    math.h is unused.

    At least the int main is correct.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    That means you have a function that hasn't been defined but is still used in your program. In this case, the function is named random. As far as I know, there isn't a function with that name anywhere. I assume that you want a random number between one and five yes? This is how you do that:
    Code:
    topf=rand()%5+1;

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Thanks ill try that, Stoned_coder, some of the header files arent used because the prog isnt done.

    EDIT: I got the same error using the other code
    Code:
    topf=rand()%5+1;
    Last edited by mattbbx; 08-14-2003 at 12:34 PM.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, my bad. Make sure you have this line after your #include's:
    Code:
    using namespace std;

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    In C rand() lives in the header stdlib.h

    In C++ std::rand() lives in the header cstdlib
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    And you should make a call to the srand() function prior to using rand(). That provides a "seed" for the random number. The results from the time function are a good one. For time you will need to include the ctime header which is the equivalent of c's time.h.
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main(void)
    {
        srand(time(NULL)); // Only call me ONCE
    
        // Do stuff like call rand() 
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM