0

I have the following code:

#include <cstdlib>
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
 int a,n,count;
 count=0; randomize();
 a=1+random(100);  
 cout<<"Enter A No. Between 1 to 100";
 do
  { 
    cin>>n;
    count++;
    if(n>a)
           cout<<"Enter a lower no.";
    else if(n<a)
           cout<<"Enter a higher no.";
    }while(n!=a);
cout<<count;

system("PAUSE");
return EXIT_SUCCESS;
}

The errors are:

  • E:\c++\main.cpp In function `int main()':
  • 10 E:\c++\main.cpp `randomize' undeclared (first use this function)
  • (Each undeclared identifier is reported only once for each function it appears in.)
  • 11 E:\c++\main.cpp `random' undeclared (first use this function)

Can anyone help me understand why these errors are occurring?

RPHerbig
  • 13
  • 3
user142187
  • 27
  • 1
  • 5
  • 1
    So far 4 totally duplicate answers :) –  Aug 24 '12 at 17:42
  • not single helpful for me as in turbo c++ this runs ok but in dev c++ – user142187 Aug 24 '12 at 17:45
  • Side note: the code doesn't need both `` and ``. – Pete Becker Aug 24 '12 at 17:45
  • @user142187 - the answers you've gotten have been very helpful. Those functions are not in standard C nor in standard C++. Using non-standard functions is okay if you don't need portability, but when you try to use multiple compilers you need to write portable code. – Pete Becker Aug 24 '12 at 17:47
  • Incidentally, although the code is, formally, C++, the random number stuff is pure C. Nothing inherently wrong with that, but there are much better random number facilities in C++, if you have TR1 or C++11 or Boost. – Pete Becker Aug 24 '12 at 17:50
  • @PeteBecker links please – user142187 Aug 24 '12 at 17:56
  • -1 The question has been answered correctly several times. SO is not an outsourcing company, so read the links and/or google `srand` and `rand` instead of expecting people to write your code for you. – smocking Aug 24 '12 at 18:00
  • got the answer thnx to all @PeteBecker can you provide links for the compilers above – user142187 Aug 24 '12 at 18:03

5 Answers5

5

randomize() is not a standard C++ function, you will have to use srand(something) to seed a random number generator, where something will ussually be current time (time(0)).

Also, random() is not a standard function, you will have to use rand()

So, something like this (cleaned up a little):

#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    srand(time(0));
    int n, count = 0;
    int a = 1 + (rand() % 100);  
    cout << "Enter A No. Between 1 to 100";
    do
    { 
        cin >> n;
        count++;
        if (n>a)
            cout << "Enter a lower no.";
        else if (n<a)
            cout << "Enter a higher no.";
    } while(n!=a);
    cout << count;

    system("PAUSE");
    return EXIT_SUCCESS;
}
  • 2
    But keep in mind, that `rand() % N` does not uniformly give a number in the range [0, N). See: http://stackoverflow.com/questions/2509679/how-to-generate-a-random-number-from-within-a-range-c – Marcus Riemer Aug 24 '12 at 17:46
  • 2
    @MarcusRiemer It is a good approximation in most cases and based on the rest of the code, an absolutely uniform random number is not needed. –  Aug 24 '12 at 17:47
3

You're using a function (here: count=0; randomize();) named "randomize" - the compiler doesn't know where to find this function, as it's not defined in your code, nor in any header you're including.

I suspect you wanted srand() and rand().


For example - you can rewrite your existing code like the following. To use this code - you'll need to also #include <time.h> in your includes:

int main()
{
 int a,n,count;
 count=0; 
 srand(time(NULL)); // use instead of "randomize"
 a = 1 + (rand() % 100); 
 // ... Rest of your code
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

The methods you're attempting to call are called srand and rand.

randomize and random are not part of the language.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 343 D:\Dev-Cpp\include\stdlib.h too few arguments to function `void srand(unsigned int)' 342 D:\Dev-Cpp\include\stdlib.h too many arguments to function `int rand()' after changing this i m getting these errors – user142187 Aug 24 '12 at 17:42
  • 1
    @user142187 I'm not going to write the code for you. Did you click on the links? Did you try to google how to properly use `srand`? – Luchian Grigore Aug 24 '12 at 17:44
  • @user142187 - yes, you need to know what arguments these functions take in order to call them. Look it up. – Pete Becker Aug 24 '12 at 17:45
1

There are no randomize() and random() functions in standard C. Maybe you mean srand() and rand()?

Take a look at this question, on how to correctly "randomise" a number in a given range. rand() % N does not uniformly give a number in the range [0, N).

Community
  • 1
  • 1
Marcus Riemer
  • 7,244
  • 8
  • 51
  • 76
1

If you have a C++11 compiler that includes <random> (if you don't, you can use boost::random from Boost library), you can use this class for better pseudo-random numbers:

#include <ctime>
#include <random>

class rng
{
private:
    std::mt19937 rng_engine;

    static rng& instance()
    {
        static rng instance_; 
        return instance_;
    }

    rng() {
        rng_engine.seed(
            static_cast<unsigned long>(time(nullptr))
            );
    };

    rng(rng const&);
    void operator=(rng const&);

public:
    static long random(long low, long high)
    {
        return std::uniform_int_distribution<long>
              (low, high)(instance().rng_engine);
    }
};

Then you use this to get random numbers in a [a,b] interval:

long a = rng::random(a, b);

You don't need to seed it manually as it will be seeded on first invocation.

  • If you're a beginner, you generally don't have to bother with such intricacies, I added this just for the sake of "doing-it-more-in-a-C++-way" –  Aug 24 '12 at 18:03