Thread: Strange error involving rand()

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Strange error involving rand()

    Just so you know, I am a begginer in C++. I have been working on a simple guessing game, but I have a problem. This is my code:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main();
    int a = rand(15);
    int doit = 8;
    int b;
    int c;
    {
        cout <<"''Good morning,'' MR MIME says cheerfully from behind the SKARMORY.\n''Would you like to play a game, RED?\nOf course you would.  Now, I will think of a NUMBER\n that could be anything from 0 to 15.\nANYTHING.\nYou will have EIGHT tries to guess the NUMBER.\nI will tell you if it is too HIGH or too LOW.\nReady, RED?''\nMR MIME begins to make odd noises while tilting his hair back.";
        cin.ignore();
        do{
                     b = rand(5);
                     if (b == 0){
                           cout<< "''Well...well...well...''  MR MIME begins to cackle menacingly.  \nWhat is your guess?''\n";
                           }
                     else if (b == 1){
                           cout<< "''What's that you say?  You want pizza?'' asks MR MIME\nwhile chugging grape juice.  ''Give me your\nguess or the girl dies!''\n";
                           }
                     else if (b == 2){
                           cout<< "''I think you've lost your mind.''  MR MIME says, taking out\na coloring book.  ''Guess, and make\nit snappy.\n";
                           }
                     else if (b == 3){
                           cout<< "''Don't want to be an American idiot!  Don't want a\nnation that does a new media!''  A sign\nduct-taped on MR MIME's torso reads: ''9v335''\n";
                           }
                     else if (b == 4){
                           cout<< "''To guess, or not to guess...That is the question!''\nMR MIME attacks with THROWING A SKULL AT YOU.\nYOU take 24 DAMAGE.\n";
                           }
                     else if (b == 5){
                           cout<< "''Green.''  MR MIME points a gun at you.\nMaybe you should guess.\n";
                           }
                     cin>> c;
                     if (c == a){
                           cout<< "CoNgRaTuLaTiOnS.  MR MIME kills himself.\n";
                           cin.get()
                           }
                     else if (c > a){
                          cout<< "MR MIME twitches his arm upwards.  He shouts, \n''700 H16H u83R n00b!!!!!!!shiftoneshiftone''\n";
                          }
                     else if (c < a){
                          cout<< "''TOOLOWTOOLOWTOOLOWTOOLOW!!!!!!!!!!!!!!!!''\nMR MIME begins to throw porcelin kittens accross the room.\n";
                          }
                     else if (c == 42){
                          cout<< "YOU WON FOR REAL!!!!! THE TRUE PRIZE IS THIS:\nhttp://www.bluefusionx.com/samusman/flash">>
                          cin.get()
                          }
                     doit = (doit - 1)
                     }while (doit > 0);
                     
                     
        
    }
    However, when I compile and run, Dev C++ Returns the following error: too many arguments to function 'int rand'

    What have I done wrong?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    rand() doesn't take any arguments. I'm not sure what the 15 is supposed to imply, but if you want to set the bounds then you can use the modulus operator.

    Code:
    num = rand() % 15 + 1;  // This will give you a number from 1 to 15
    Last edited by SlyMaelstrom; 05-13-2006 at 07:48 PM.
    Sent from my iPadŽ

  3. #3
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Rand doesn't take any parameters, that's the problem
    http://cplusplus.com/ref/cstdlib/rand.html

    rand() returns an integer between 0 and RAND_MAX

    int a = rand()%15 to create a number from 0-14 instead

    *edit*bah, beaten to it*

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    2
    ah, thank you very much. I am used to the rand being formatted differently in other lanquages.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    By the way, rand() will always produce the same set of numbers over and over again each time your program is run. Google up srand().


    [edit]srand()
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

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. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. rand() to choose?
    By wagman in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 01:43 AM