d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Call A Void Statement Using A X Variable
Add Reply New Topic New Poll
Member
Posts: 6,635
Joined: Jun 27 2010
Gold: 2.00
May 11 2014 01:47pm
for example I have the void function

void chance1()

and also

void chance2()

And i want to call one of the functions based on the user choices

is their a way to put in a x variable for 2 and 1 in order to not make so many if statements

so i want to do something like

cout << "blah";
cin >> x;
chance[x]() // this is what i'm trying but it is obviously not working...giving me "chance was not declared in scope"

lmk if you need more explanation.
Member
Posts: 6,635
Joined: Jun 27 2010
Gold: 2.00
May 11 2014 01:57pm
4fg if your suggestion works!!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 11 2014 02:00pm
Quote (thebigman1212 @ May 11 2014 03:47pm)
for example I have the void function

void chance1()

and also

void chance2()

And i want to call one of the functions based on the user choices

is their a way to put in a x variable for 2 and 1 in order to not make so many if statements

so i want to do something like

cout << "blah";
cin >> x;
chance[x]() // this is what i'm trying but it is obviously not working...giving me "chance was not declared in scope"

lmk if you need more explanation.


if you're trying to build the function name as a string and invoke it, i wouldn't recommend it, but i think it's possible. it's called reflection in c#, dunno about c.

you could create an array of function pointers then use that idea (which has nothing to do with the function names), but i also wouldn't recommend it. dunno the exact syntax, but something similar to this i imagine:
int (*functions[2])();
functions[0] = chance0;
functions[1] = chance1;

then to call it, (*functions[x])();

just use if statements. i assume you're making some sort of menu with n choices and each choice does something different. if they're all something similar, you can likely refactor it.

This post was edited by carteblanche on May 11 2014 02:03pm
Member
Posts: 6,635
Joined: Jun 27 2010
Gold: 2.00
May 11 2014 02:04pm
Quote (carteblanche @ May 11 2014 03:00pm)
if you're trying to build the function name as a string and invoke it, i wouldn't recommend it, but i think it's possible.

you could create an array of function pointers then use that idea (which has nothing to do with the function names), but i also wouldn't recommend it.

just use if statements. i assume you're making some sort of menu with n choices and each choice does something different. if they're all something similar, you can likely refactor it.


yeah i guess so, might just do if statements which are easy to copy and paste...just ALOT more lines of code hah
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 11 2014 02:07pm
Quote (thebigman1212 @ May 11 2014 04:04pm)
yeah i guess so, might just do if statements which are easy to copy and paste...just ALOT more lines of code hah


is your logic very different or similar? i can't understanding having 1000 different choices for your assignment. 5-10 shouldn't be a problem.

you said "chance" instead of "choice", so i'm not clear what you're doing. if it's math related, you can likely refactor it.

if you use many parallel functions, you can prolly refactor it into classes. just have one method that determines which class to create via factory, then each class can have its own functionality.

This post was edited by carteblanche on May 11 2014 02:09pm
Member
Posts: 6,635
Joined: Jun 27 2010
Gold: 2.00
May 11 2014 02:16pm
well basically it's a small card game

(the program is for a introduction to c++ online class)

it involves having 10 void statements with the design of the cards

The first card is picked for the computer out of the 10.

then the user gets to pick a card from the 10 choices.

so I have

if choice = 1
{

10 if statements

}

if choice = 2
{
10 if statements
}

etc...

If i could just call the void based on the user choice, then I would only need 1 if statement for each choice statement

My voids have no crucial info, just design
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 11 2014 02:22pm
Quote (carteblanche @ 11 May 2014 22:00)
if you're trying to build the function name as a string and invoke it, i wouldn't recommend it, but i think it's possible. it's called reflection in c#, dunno about c.

you could create an array of function pointers then use that idea (which has nothing to do with the function names), but i also wouldn't recommend it. dunno the exact syntax, but something similar to this i imagine:
int (*functions[2])();
functions[0] = chance0;
functions[1] = chance1;

then to call it, (*functions[x])();

just use if statements. i assume you're making some sort of menu with n choices and each choice does something different. if they're all something similar, you can likely refactor it.

Your code is correct, but ugly. It'd be better to typedef it away
Code
typedef int(*ptr_to_func_returning_int)();
ptr_to_func_returning_int functions[2];

Or, in C++11 and later you can use alias templates (e.g. as exemplified here: http://dev.krzaq.cc/readable-function-pointers/ )

If you want to keep state or don't care about performance (as in having an overhead of a virtual call), use std::function ( http://en.cppreference.com/w/cpp/utility/functional/function ) with it's more readable syntax.

This post was edited by KrzaQ2 on May 11 2014 02:23pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 11 2014 02:46pm
Quote (thebigman1212 @ May 11 2014 04:16pm)
well basically it's a small card game

(the program is for a introduction to c++ online class)

it involves having 10 void statements with the design of the cards

The first card is picked for the computer out of the 10.

then the user gets to pick a card from the 10 choices.

so I have

if choice = 1
{

10 if statements

}

if choice = 2
{
10 if statements
}

etc...

If i could just call the void based on the user choice, then I would only need 1 if statement for each  choice statement

My voids have no crucial info, just design


i'm still not clear on what you're doing. if i understand you correctly, there's a 52 card deck, and 10 cards are dealt on the table. then the computer chooses 1, then player chooses one, repeat? i don't see how that equates to 10 choices x 10 more if statements = 100 if statements. show some concrete code and we can prolly refactor it.

if those 10 inner if statements to determine which function to call are always the same, then just have one function with the 10 if statements, then in each block call that one function.

eg:
Code

void delegate(int choice){
if (choice == 1){
function1();
} else if (choice == 2){
function2();
}
.....
}

if choice = 1
{
delegate(1);
}

if choice = 2
{
delegate(2);
}

or just skip that altogether and call delegate(choice);

again, i have to see what your code is to give more input.

Quote
Your code is correct


sheer luck :thumbsup:

This post was edited by carteblanche on May 11 2014 02:50pm
Member
Posts: 9,525
Joined: Nov 5 2005
Gold: 1,338.00
May 11 2014 03:14pm
Well, to be honest I have no idea what you're doing, but based on what you've said, you could just make a function that chooses a function. That way you can cut down on lines of code, as you wanted.

For example,

if you have ten chance functions,

Code
void chance1()
void chance2()
..
void chance10()


you could have a chooseFunc function which just takes an int 1-10 and activates the respective function.

for example:

Code
void chooseFunc(int choice) {
if (choice == 1) chance1();
else if (choice == 2) chance2();
....
else if (choice == 10) chance10();
}

Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 11 2014 03:25pm
In this case, using a function pointer instead of an if-tree would be a better choice. It's easier to maintain, clearer (for a large tree, especially if you sometimes want to re-use a function, otherwise there's little difference) and potentially a tad faster (depending on how smart your compiler is).
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll