Thread: Fast random quotes

  1. #1
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209

    Fast random quotes

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <windows.h>
    #include <conio.c>
    
    int rand_mid(int low, int high) 
    {
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }
    
    int randomnumber = rand_mid(1,10);
    char declaration(randomnumber);
    char *point =& declaration(randomnumber);   // ERROR HERE
    
    char declaration1[20] = "ONE";
    char declaration2[20] = "TWO";
    char declaration3[20] = "THREE";
    char declaration4[20] = "FOUR";
    char declaration5[20] = "FIVE";
    char declaration6[20] = "SIX";
    char declaration7[20] = "SEVEN";
    char declaration8[20] = "EIGHT";
    char declaration9[20] = "NINE";
    char declaration10[20] = "TEN";
    
    int main()
    {
    cout << point;
    Sleep(1000);
    return 0;
    }
    I want this to write randomly "ONE", "TWO", etc., however if gives me an error, where indicated, saying "declaration cannot be used as a function"

    Any ideas ?

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    There are many errors in your code. Here's a way to solve the problem:
    Code:
    char declaration[10][] = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
    
    
    cout << declaration[ rand()%10 ];
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    This still has errors :

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <windows.h>
    #include <conio.c>
    
    int rand_mid(int low, int high) 
    {
    srand(time(NULL));
    return low + rand()%(high-low+1);
    }
    
    char dec[10][] = {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};  // ERROR
    
    int main()
    {
    cout << dec[rand_mid(1,10)] << endl << endl;  // ERROR 2
    system("pause");
    
    return 0;
    }



    I get the following error :

    declaration of 'dec' as multidimensional array
    must have bounds for all dimensions except the first
    assignment (not initialization) in declaration

    Error 2 :

    ANSI C++ forbids using pointer to a function in arithmetic


    What's wrong now ? Thanks.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>char dec[10][]
    C++ requires that you supply the size of all dimensions except the first. But here it's better to just use an array of char *'s so that you don't have to worry about the second dimension.

    >>srand(time(NULL));
    Don't call srand() in the same function as rand(), if you do it'll reseed the generator every time you call the function and your random numbers won't be random at all.

    >>dec[rand_mid(1,10)]
    This is an off by one error, the array only has indices from 0-9.

    >>ANSI C++ forbids using pointer to a function in arithmetic
    dec is a modifier for cout in C++, this name clash is causing your error, you should change the name of the array.

    On top of all this you still neglected to include stdlib.h, which is required for rand() and srand(). :-) Try this, it should work better.
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int rand_mid(int low, int high)
    {
      return low + rand()%(high-low+1);
    }
    
    char *decl[] = {
      "ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"
    };
    
    int main()
    {
      srand(time(NULL));
      cout << decl[rand_mid(0,9)] << endl << endl;
      system("pause");
    }
    *Cela*

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    You don't need the rand_mid function, do you? Just use this:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
      srand(time(NULL));
    
      char *decl[] = {
        "ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","N  INE","TEN"
      };
    
      cout << decl[rand()%10] << "\n\n";
      system("pause");
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Thanks a lot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fast normal random number generator
    By testing123 in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2006, 10:01 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. fast random number generation
    By Heraclitus in forum C Programming
    Replies: 4
    Last Post: 02-09-2003, 07:48 PM
  4. The great struggle for random quotes!
    By Jeremy G in forum Game Programming
    Replies: 2
    Last Post: 01-26-2003, 01:28 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM