returning fuctions and a switch loop

Started by
9 comments, last by Jiia 18 years, 10 months ago
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
visit my site http://www.fullf1.com for all your formula 1 and music creation needs.
Advertisement
real code, wrapped in [ source] or blocks is going to make understanding your exact problem a LOT easier.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This should do what you're after (assuming I understand your question correctly):

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

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

You might want to repost and make it more clear.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                          
Looking for video game music? Check out some of my samples at http://www.youtube.c...ser/cminortunes            
                                                          
I'm currently looking to create music for a project, if you are interested e-mail me at cminortunes@gmail.com    
                                                          
Please only message me for hobby projects, I am not looking to create music for anything serious.
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 :/
visit my site http://www.fullf1.com for all your formula 1 and music creation needs.
Well, in that case, store the number before entering the switch:
int num = GetNum();switch(num) {...}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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
visit my site http://www.fullf1.com for all your formula 1 and music creation needs.
Quote:Original post by rholding2001
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
*** 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;}
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.

[edit] Beaten to it by bitmap... [/edit]
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.

thanks for the reply tho.

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 :)
visit my site http://www.fullf1.com for all your formula 1 and music creation needs.

This topic is closed to new replies.

Advertisement