"Pointer to" function type

How do I correctly use a pointer to function type? Im running a program thats shuffling numbers for me, and I defined my result, set the char string, set random_shuffle. And it says I need a pointer to function type, any help would be nice. :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <time.h>
#include <cstdlib>

using namespace std;
#define result random_shuffle(&cards[0],&cards[12]);

int main()
{
	int result;
	char cards [] = "h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12";
	srand(time(NULL));
	random_shuffle(&cards[0],&cards[12]);
	cout << result;
	system("PAUSE");
	return 0;
}
random_shuffle() returns void (in other words, nothing), so you can't send it to cout as you are on line 17. Use a for loop to print out the array instead.

Now that said, you have some other problems as well. That character array is well of 12 elements long. To show you, the first couple elements are: 'h' '1' ',' ' ' 'h' '2' and so on. So your random shuffle will only shuffle the first 13 elements.

Also, it seems you misunderstand what #define does. It simply replaces some text with some other text before the code is compiled. So you end up with a line 13 that says:
int random_shuffle(&cards[0], &cards[12]);
And a line 17 that says:
cout << random_shuffle(&cards[0], &cards[12]);
Neither of these are what you want.
Awesome, thanks man. I know what define does, I just thought Id try something out.. But it didnt work. =/ So, take out #define int result. And set a for loop. I'll do that and show you what it looks like, and if theres any errors.
You cant make a for loop with char though, so I would have to make an integer...
A. yes you can
B. no you shouldn't.
Do you remember how to make basic for-loops?
Actually, you can, though that isn't what I was saying to do.
Use a for loop to iterate over and display the character array.
1
2
3
for(unsigned int i = 0; i < length; ++i) {
    do_something(array[i]);
}


In fact,
1
2
3
for(char i = 0; i < length; ++i) {
    do_something(array[i]);
}

This however, assumes that length will be within the bounds of a char. I don't recommend this at all.
L B, a little bit. I think I figured it out, I put the basic for loop in to print it out. Gimme a sec.
Last edited on
It doesnt work, completely.
2 Problems, h4 and above arent mixed. But before that, h1-h3 are scrambled.Letters and all..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>
#include <time.h>

using namespace std;

int main()
{
	char i [] = "h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12";
	for (char i= 0; i < 13; i++)
	srand(time(NULL));
	random_shuffle(&i[0],&i[12]);
    cout << i << endl;
	system("PAUSE");
	return 0;
}
Last edited on
Reread Zhuge's post, he clearly explained exactly why.
Topic archived. No new replies allowed.