returning fuctions and a switch loop
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
real code, wrapped in [ source] or
blocks is going to make understanding your exact problem a LOT easier.
This should do what you're after (assuming I understand your question correctly):
int result = instance.genNum();SomeOtherFunction(result);
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 :/
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 :/
Well, in that case, store the number before entering the switch:
int num = GetNum();switch(num) {...}
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
whats happeneing is that 3 and 4 are not beiong recognised. what am i doing wrong?
thanks people
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
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:
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]
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 :)
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 :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement