Whats wrong with this function?

Started by
7 comments, last by PunaProgrammer chris 18 years, 8 months ago
void random () { random = rand(); if(random == 5) { SDL_BlitSurface(rect,NULL,screen,&rectangle); if(random == 6) SDL_BlitSurface(square,NULL,screen,&sqr); if(random == 7) SDL_BlitSurface(rightangle,NULL,screen,righta); else random = rand(); } } It gives me this error when I use it: void random () { random = rand(); if(random == 5) { SDL_BlitSurface(rect,NULL,screen,&rectangle); if(random == 6) SDL_BlitSurface(square,NULL,screen,&sqr); if(random == 7) SDL_BlitSurface(rightangle,NULL,screen,righta); else random = rand(); } } term does not evaluate to a function taking 0 arguments
Advertisement
What type of variable is "random" ?

If random == 5, how can it == 6? Or 7?

PS:

You should mod rand() to limit its range, because there is no way to guarantee that any of those numbers will be generated, which can lead to an infinite loop (or a really long one) and uneven delays depending on how you use it. I've been known to be wrong in the past, though.
skulldrudgery--A tricky bit of toil
Oops. I am pretty sure that you can't use the same identifier for a variable and a function name. You are using "random" as a variable (or trying to use a recursive function very incorrectly). In the function, you are trying to assign a random value to a function (or a pointer to the place in memory the function begins?). Anyway, you can't do that.
I think the error is because the compiler thinks you are trying to use a function pointer to function that does not have the same return type and number of arguments.

Furthermore, random returns void, so using "random" in an if statement wouldn't evaluate at all.

Edit: Ok, I'm wrong about that identifier thing. That is only if the variable is global.

Edit2: Ok, if you don't declare a variable called "random" in the function, the compiler thinks you are referring to the function.
skulldrudgery--A tricky bit of toil
In C++:

- If you want to return something from a function, you must declare the type of the thing you want to return - in the spot before the function name. "void" there means nothing will be returned; i.e. the function just "does something", and does not produce a value that could be assigned somewhere else.

- To specify the value to return, you must put the expression directly in a return statement; there is no implicit variable named after the function to assign to. (In a void function, you can either 'return;' explicitly, or just let the function get all the way to the end - it is preferred that you not explicitly put 'return;' at the end of a void function, because it is useless.)

- To create a separate variable, you need to specify its type in its declaration, and you won't be able to give it the same name as the function.

- The standard library 'rand()' function returns a value from 0 up to RAND_MAX inclusive, and RAND_MAX is typically a large number - thus you'll pick 5, 6, or 7 only very rarely, unless you do some mapping with the rand() value to get a number from a smaller range.
Problems:

1)If random is a global variable it won't work here, as it is getting replaced by the function random since it has the same name. You could make a local variable named random, and that would work.

2)You have the if random equals 6 and 7 statements inside the if random==5 statement. If random equals 5, it cant equal 6 or 7.

3)The else statement only corresponds with the last if statement. If either of the first two were true, the else statment would still execute. This is fixed by changing the second and third if to else if.

4)Your random number needs to have a limit. Do this by modding (%) rand by the highestnumber you want it to go up to, then adding one. Example: You want a random number between 1 and 7. So you go: int i = (rand()%7)+1.

Here is the source with changes done by me:

void random (){     int max = //put the max number here    int random = (rand()%max)+1;    if(random == 5)        SDL_BlitSurface(rect,NULL,screen,&rectangle);    else if(random == 6)        SDL_BlitSurface(square,NULL,screen,&sqr);    else if(random == 7)        SDL_BlitSurface(rightangle,NULL,screen,righta);    else        random = (rand()%max)+1;}


Also, I'm not sure if you want to return the last rand or not, but it seems like you would.
edit: fixed source code
Also, I'm a bit offtopic but you might want to change your rand. Because it will usualy give the same number.

all you have to do is include <time.h>

and change you code like that

srand(time(0));
iRandom = rand()%7+1;

It's a random from 1 to 7.
Quote:Original post by deathwearer
Also, I'm a bit offtopic but you might want to change your rand. Because it will usualy give the same number.

all you have to do is include <time.h>

and change you code like that

srand(time(0));
iRandom = rand()%7+1;

It's a random from 1 to 7.


He could've already called srand in another function, or globally.
But it still good to point out since the user seems new to programming and might not know about srand. Don't assume the OP has done anything when helping solve issues. Doing so limits your scope of thinking.
Good point.

This topic is closed to new replies.

Advertisement