Jump to content
  • Advertisement
Sign in to follow this  
rholding2001

returning fuctions and a switch loop

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


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

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
Well, in that case, store the number before entering the switch:

int num = GetNum();
switch(num) {
...
}

Share this post


Link to post
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 this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
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.

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

Share this post


Link to post
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.

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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!