Sign in to follow this  
rholding2001

returning fuctions and a switch loop

Recommended Posts

rholding2001    145
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
Washu    7829
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
rholding2001    145
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
Washu    7829
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
rholding2001    145
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
Sam Gamgee    222
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
rholding2001    145
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
Jiia    592
Is this what you wanted to do with a switch statement?


switch( num )
{
case 1:
case 2:
value = value * 5;
cout << "you got 1 or 2" << endl;
break;

case 3:
case 4:
value = value * 2
cout << "you got 3 or 4" << endl;
break;

default:
cout << "the number was not 1 2 3 or 4" << endl;
break;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this