Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

compiler/callfunction

Name: Anonymous 2010-11-05 22:55

Hurr, getting into the basics of C++ here, and running into issues with calling user defined functions more often that I feel I should. I wrote a mock program to test the issue, and I'm still running into trouble:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;
int randGen ();

int main ()
{
    srand ( time ( 0 ) );
    cout << randGen;
    cin.ignore().get();
    return EXIT_SUCCESS;
}

int randGen ()
{
    srand ( time ( 0 ) );
    return ( 1 + rand ( ) % 10);
}

It compiles, but it tells me "[Warning] the address of `int randGen()', will always evaluate as `true' " and the only thing it outputs is "1" when I want it to output a random number between 1 and 10.

help?

Name: Anonymous 2010-11-05 23:07

Stop using C++.

Name: Anonymous 2010-11-05 23:09

Start using Perl.

Name: Anonymous 2010-11-05 23:10

>>1
Maybe you call the function instead of printing it. And stop using C++.

Name: Anonymous 2010-11-05 23:11

What's wrong with C++?

Name: Anonymous 2010-11-05 23:29

Name: Anonymous 2010-11-05 23:40

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

using namespace std;
int randGen ();

int main ()
{
    cout << randGen();
    return 0;
}

int randGen ()
{
    srand ( time(NULL) );
    return ( 1 + rand ( ) % 10);
}

Name: Anonymous 2010-11-05 23:42

>>7
YOU HELPED HIM!

Name: Anonymous 2010-11-06 0:46

no exceptions!

Name: Anonymous 2010-11-06 1:15

{code]
#!/usr/bin/env perl
use strict;
use warnings;
               
sub GetRand{
my $num = rand ();
return $num;   
};

my $number = 10 * &GetRand();                          
printf ("%u" , $number );
[/code]

Name: Anonymous 2010-11-06 1:17

fffffuuuuuBBCODE

Name: Anonymous 2010-11-06 5:51

>>10-11
IABTR

Name: Anonymous 2010-11-06 6:42

ONLY SRAND ONCE DAMMIT

Name: Anonymous 2010-11-06 9:09

I prefer to start with mpiler which is dual to compiler.

Name: Anonymous 2010-11-06 9:57

"[Warning] the address of `int randGen()', will always evaluate as `true' "
What if you setup your link script to put the function at 0x00000000?

Name: Anonymous 2010-11-06 10:36

>>7

When I did that it works, but if I try output the value multiple times in a loop, I'd get the same randomly generated number for each time it was called.

Name: VIPPER 2010-11-06 11:36

lol sepples

Name: Anonymous 2010-11-06 13:09

>>16
Yeah, that's expected behaviour. Random numbers will do that some times. Because they're random.

Name: Anonymous 2010-11-06 21:39

>>18
an array of 30 random numbers shouldn't all be the same number. and a rerun of the program will result in a different number than the initial run but all 30 outputs will be the same number.

that's not random, there's some flaw in the way the program is grabbing the numbers.

Name: Anonymous 2010-11-06 22:24

>>19
Well, see, that's just the thing. By the Law of Truly Large Numbers, if enough people run that program, sooner or later any improbable sequence would have happened. It just so happens you were the lucky one.

Name: Anonymous 2010-11-07 0:26

>>16
Maybe because you're seeding using time and your processor is too fast. Try hitting your turbo button before executing your code.

Name: Anonymous 2010-11-07 1:25

Oh, I see! So rand() can only make new numbers so fast.
I added system("sleep 1") to randGen, and it works fine now
thanks, everyone!

Name: Anonymous 2010-11-07 1:45

>>22
IHBT

Name: Anonymous 2010-11-07 14:44

>>22
system("sleep 1")
`INHOUSE SOFTWARE WRITTEN BY AN INTERN FOR A SMALL TO MEDIUM SIZED COMPANY' - QUALITY

Name: Anonymous 2010-11-07 14:47

/prog/ is so helpful.

Name: Anonymous 2010-11-07 16:48

HELP MY ANUS

Name: Anonymous 2010-11-07 17:39

I'm actually feeling helpful today. Take out system("sleep 1") and never use that again. Ever. Then move srand ( time(NULL) ); from randGen to the beginning of main.

Name: Anonymous 2010-11-07 17:59

#define system(...) printf(__VA_ARGS__), exit (255)

or even just

#define system

Name: Anonymous 2010-11-07 18:00

>>27
That's not being helpful, that's sabotage of our fine efforts.

Name: Anonymous 2010-11-07 19:11

>>28
#define system(a) assert(!a)

Name: Anonymous 2010-11-07 20:14

#define system(s) system(s)

Name: Anonymous 2010-11-07 20:58

>>30
system(0);

Name: Anonymous 2011-01-31 20:53

<-- check em dubz

Name: Anonymous 2011-02-04 12:37


Don't change these.
Name: Email:
Entire Thread Thread List