# returning fuctions and a switch loop

This topic is 4977 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi there im just wondring how to do this my program at the moment generates a random number in a class, for eg the gennum function within the class would look like int class::genNum(); { int num = rand() 50; return num; } in a switch statement i want to put something like case 1: classinstance.genNum() after this has been called i want to take the return value of the genNum() funtion and put it into another function that displays an action for the number the problem i get is is i try to do int resultnum = classsinstance.genNum() classinstance genNum(resultnum) genNum gets called again and i dont want it to. How do i put the result of genNum into anotehr function without calling it? thanks for the help

##### Share on other sites
real code, wrapped in [ source] or [ code] blocks is going to make understanding your exact problem a LOT easier.

##### Share on other sites
This should do what you're after (assuming I understand your question correctly):

int result = instance.genNum();SomeOtherFunction(result);

##### Share on other sites
You might want to repost and make it more clear.

##### Share on other sites
yeah i dont think ive made myself very clear and i cant explain it better.

the problem i have is that i need to use the return value of a funtion.
using this return value using something like num = afunc() calls afunc again.

the user themeselvces calls afunc() in a swirch statement . . i think ive made some further modifications thats getting it better . . .now i seem to be having problems with my nested ifs :/

##### Share on other sites
Well, in that case, store the number before entering the switch:
int num = GetNum();switch(num) {...}

##### Share on other sites
thanks people for helping me. in the end i decided to make the returning function a void i am however stuck with these nested if's

could anyone suggest to me why this isnt working please
void CGame::wintable(int num){		if((num == 1)||(num == 2))	{				value = value * 5;			cout << "you got 1 or 2" << endl;				if((num == 3)||(num == 4))	{				value = value * 2			cout <<  "you got 3 or 4" << endl;	}	}	else cout << "the number was not 1 2 3 or 4" << endl;}

whats happeneing is that 3 and 4 are not beiong recognised. what am i doing wrong?

thanks people

##### Share on other sites
Quote:
 Original post by rholding2001thanks people for helping me. in the end i decided to make the returning function a void i am however stuck with these nested if'scould anyone suggest to me why this isnt working please*** Source Snippet Removed ***whats happeneing is that 3 and 4 are not beiong recognised. what am i doing wrong?thanks people

You're missing an end bracket } after your first if statement.

EDIT: So it should look like this:
void CGame::wintable(int num){		if((num == 1)||(num == 2))	{				value = value * 5;			cout << "you got 1 or 2" << endl;	}				else if((num == 3)||(num == 4))	{				value = value * 2			cout <<  "you got 3 or 4" << endl;	}		else cout << "the number was not 1 2 3 or 4" << endl;}

##### Share on other sites
You missed a closing brace after checking for 1 and 2. Right now, you check to see if it's 1 or 2 and only if it is, then you check for 3 and 4. The code should be:

void CGame::wintable(int num){		if((num == 1)||(num == 2))	{				value = value * 5;			cout << "you got 1 or 2" << endl;        }	else if((num == 3)||(num == 4))	{				value = value * 2			cout <<  "you got 3 or 4" << endl;	}	else cout << "the number was not 1 2 3 or 4" << endl;}

Notice now that we check to see if it's 3 or 4 only if it isn't 1 or 2.

 Beaten to it by bitmap... [/edit]

##### Share on other sites
yes that does indeed make it work. the preoblem with it however is if 1 or 2 is the answer then you get both the cout << you got 1 or 2 AND cout << you didnt get 1234.

im sure there are elses to be put in somehwere ive tried many thibgs and comparing to other examples but the ones ive tried just havent been successful.

edit.

yeah . . silly me, i did that but there was another problem that i hadnt seen doh . . thanks man really helpful . . the both of you. thanks a lot :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 12
• 10
• 11
• 13
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015480
×