Looping Voids Will Not Work

Jan 16, 2012 at 9:52pm
Hi. I have a program that is running using multiple void functions, but I want it to repeat. It worked perfectly before I tried to ad the loop statement, but now refuses to work with it. It will always highlight the parenthesis after the VoidSetCards, and I do not understand why. Every parenthesis is matched, as I added two after creating the loop.
Error:
error C2601: 'createSetCards' : local function definitions are illegal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <iomanip>
using namespace std;
void createSetCards();
void shuffle();
void display();
void findSet();
void display12();

int cards[81];
int cardPosition[81];

int main()
{
	createSetCards();
	display();
	shuffle();
	cout<<endl<<endl<<endl;
	display12();
	findSet();
	
	system("pause");
	return 0;
}

void createSetCards()
{
	int i =0;
	for(int a = 1; a < 4; a++)
	{
		for(int b = 1; b < 4; b++)
		{
			for(int c = 1; c < 4; c++)
			{
				for(int d = 1; d < 4; d++)
				{
					cards[i]=1000*a+100*b+10*c+d;
					i++;
				}
			}
		}
	}

	return;
}

void display()
{
	for(int j=0; j<81; j++)
	{
		cout << setw(7) << cards[j];
		if(j%9==8)
		{
			cout<<endl;
		}
	}


	return;
}

void shuffle()
{
	srand(time(0));
	for(int q=0; q<81; q++)
	{
		cardPosition[q]=rand()%81;
		for(int k=0; k<q; k++)
		{
			if(cardPosition[q]==cardPosition[k])
			{
				q--;
				k=q;
			}
		}
	}
	return;
}

void display12()
{
	for(int w=0; w<12; w++)
	{
		cout<<setw(7)<<cards[cardPosition[w]];
		if (w%6==5)
		{
			cout<<endl;
		}
	}
	cout<<endl<<endl;
	return;
}

void findSet()
{
	int flag=0;
	for(int x=0; x<10; x++)
	{
		for(int y=x+1; y<11; y++)
		{
			for(int z=y+1; z<12; z++)
			{
				int total,t1000,t100,t10,t;
				total = cards[cardPosition[x]] +cards[cardPosition[y]] +cards[cardPosition[z]];
				t1000=total/1000;
				total=total%1000;
				t100=total/100;
				total=total%100;
				t10=total/10;
				t=total%10;
				if(t1000%3==0 && t100%3==0 && t10%3==0 & t%3==0)
				{
					cout<<cards[cardPosition[x]]<<setw(7)<<cards[cardPosition[y]]<<setw(7)<<cards[cardPosition[z]]
						<<" is a SET"<<endl;
						x=y=z=12;
						flag=1;
				}
			}
		}
	}
	if(flag==0)
	{
		cout<<" No set found"<<endl;
	}

	return;
			{
Last edited on Jan 16, 2012 at 10:41pm
Jan 16, 2012 at 10:01pm
Define the function outside main(), and make sure it is declared before you call it.
Last edited on Jan 16, 2012 at 10:02pm
Jan 16, 2012 at 10:28pm
The function is declared outside of main before, I just didn't include it because I didn't want to post the whole of the code. This is just a section.
Last edited on Jan 16, 2012 at 10:32pm
Jan 16, 2012 at 10:31pm
1
2
3
4
5
void createSetCards()
		{
		int i =0;
		for(int a = 1; a < 4; a++)
			{


This is wrong. You can't define functions inside main().
Jan 16, 2012 at 10:41pm
I edited it to include the whole of the code, maybe that will clarify.
Jan 16, 2012 at 10:52pm
I tried it and it works, but there are some simple mistakes. The closing brace at the end, and you should #include <ctime> and #include <cstdlib> and #include <windows.h> .
Last edited on Jan 16, 2012 at 10:54pm
Jan 16, 2012 at 10:59pm
Why should he include windows.h?
Jan 16, 2012 at 11:00pm
Yes, so my original version is fine.However, when I attempt to add a loop of the whole program, I simply cannot get it to properly function. The edited code is below, with the changes underlined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <iomanip>
using namespace std;
void createSetCards();
void shuffle();
void display();
void findSet();
void display12();

int cards[81];
int cardPosition[81];

int main()
{
	for(int b=0;b<1000;b++)
	{
		{
			createSetCards();
			display();
			shuffle();
			cout<<endl<<endl<<endl;
			display12();
			findSet();
	
			system("pause");
			return 0;
		}

	void createSetCards()
	{
		int i =0;
		for(int a = 1; a < 4; a++)
		{
			for(int b = 1; b < 4; b++)
			{
				for(int c = 1; c < 4; c++)
				{
					for(int d = 1; d < 4; d++)
					{
						cards[i]=1000*a+100*b+10*c+d;
						i++;
					}
				}
			}
		}

		return;
	}

	void display()
	{
		for(int j=0; j<81; j++)
		{
			cout << setw(7) << cards[j];
			if(j%9==8)
			{
				cout<<endl;
			}
		}


		return;
	}

	void shuffle()
	{
		srand(time(0));
		for(int q=0; q<81; q++)
		{
			cardPosition[q]=rand()%81;
			for(int k=0; k<q; k++)
			{
				if(cardPosition[q]==cardPosition[k])
				{
					q--;
					k=q;
				}
			}
		}
		return;
	}

	void display12()
	{
		for(int w=0; w<12; w++)
		{
			cout<<setw(7)<<cards[cardPosition[w]];
			if (w%6==5)
			{
				cout<<endl;
			}
		}
		cout<<endl<<endl;
		return;
	}

	void findSet()
	{
		int flag=0;
		for(int x=0; x<10; x++)
		{
			for(int y=x+1; y<11; y++)
			{
				for(int z=y+1; z<12; z++)
				{
					int total,t1000,t100,t10,t;
					total = cards[cardPosition[x]] +cards[cardPosition[y]] +cards[cardPosition[z]];
					t1000=total/1000;
					total=total%1000;
					t100=total/100;
					total=total%100;
					t10=total/10;
					t=total%10;
					if(t1000%3==0 && t100%3==0 && t10%3==0 & t%3==0)
					{
						cout<<cards[cardPosition[x]]<<setw(7)<<cards[cardPosition[y]]<<setw(7)<<cards[cardPosition[z]]
							<<" is a SET"<<endl;
							x=y=z=12;
							flag=1;
					}
				}
			}
		}
		if(flag==0)
		{
			cout<<" No set found"<<endl;
		}

		return;
	}
}
Last edited on Jan 16, 2012 at 11:00pm
Jan 16, 2012 at 11:01pm
Only put the function calls inside the loop. The function definitions should still be outside main.
Jan 16, 2012 at 11:04pm
I thought <windows.h> is needed for system("pause"). It won't hurt anyway, though system("pause") should be avoided.
Jan 16, 2012 at 11:09pm
It hurts if you don't use Windows. std::system is a function declared in <cstdlib>.
Jan 16, 2012 at 11:11pm
system commands should be avoided when making pograms for wider use.If he is making the program for himself they are fine
Jan 16, 2012 at 11:42pm
Peter87, don't function definitions always have to follow the function call? How can I place the function calls exclusively within the loop?
Jan 17, 2012 at 12:58am
My most recent attempt builds, but it does not successfully execute the code 1000 times.
Clearly I'm not getting something here.
Edit: What was changed was in bold

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <iomanip>
#include <iostream>
#include <iomanip>
using namespace std;
void repeat();
void createSetCards();
void shuffle();
void display();
void findSet();
void display12();

int cards[81];
int cardPosition[81];

int main()
{
	createSetCards();
	display();
	shuffle();
	cout<<endl<<endl<<endl;
	display12();
	findSet();
	for(int d=0;d<1000;d++)
	{
		void repeat();
		void createSetCards();
		void shuffle();
		void display();
		void findSet();
		void display12();
	}
	system("pause");
	return 0;
}
	
void createSetCards()
{
	int i =0;
	for(int a = 1; a < 4; a++)
	{
		for(int b = 1; b < 4; b++)
		{
			for(int c = 1; c < 4; c++)
			{
				for(int d = 1; d < 4; d++)
				{
					cards[i]=1000*a+100*b+10*c+d;
					i++;
				}
			}
		}
	}

	return;
}

void display()
{
	for(int j=0; j<81; j++)
	{
		cout << setw(7) << cards[j];
		if(j%9==8)
		{
			cout<<endl;
		}
	}


	return;
}

void shuffle()
{
	srand(time(0));
	for(int q=0; q<81; q++)
	{
		cardPosition[q]=rand()%81;
		for(int k=0; k<q; k++)
		{
			if(cardPosition[q]==cardPosition[k])
			{
				q--;
				k=q;
			}
		}
	}
	return;
}

void display12()
{
	for(int w=0; w<12; w++)
	{
		cout<<setw(7)<<cards[cardPosition[w]];
		if (w%6==5)
		{
			cout<<endl;
		}
	}
	cout<<endl<<endl;
	return;
}

void findSet()
{
	int flag=0;
	for(int x=0; x<10; x++)
	{
		for(int y=x+1; y<11; y++)
		{
			for(int z=y+1; z<12; z++)
			{
				int total,t1000,t100,t10,t;
				total = cards[cardPosition[x]] +cards[cardPosition[y]] +cards[cardPosition[z]];
				t1000=total/1000;
				total=total%1000;
				t100=total/100;
				total=total%100;
				t10=total/10;
				t=total%10;
				if(t1000%3==0 && t100%3==0 && t10%3==0 & t%3==0)
				{
					cout<<cards[cardPosition[x]]<<setw(7)<<cards[cardPosition[y]]<<setw(7)<<cards[cardPosition[z]]
						<<" is a SET"<<endl;
						x=y=z=12;
						flag=1;
				}
			}
		}
	}
	if(flag==0)
	{
		cout<<" No set found"<<endl;
	}
	void repeat();

	return;
}
Last edited on Jan 17, 2012 at 1:03am
Jan 17, 2012 at 1:04am
I suppose you were trying to call a function here:
1
2
3
4
5
6
void repeat();
          void createSetCards();
	  void shuffle();
          void display();
	  void findSet();
          void display12();


But, when calling a function, you musn't precede it by it's type.
It should be like this:
1
2
3
4
5
6
repeat();
          createSetCards();
          shuffle();
	  display();
	  findSet();
		display12();


Last edited on Jan 17, 2012 at 1:06am
Jan 17, 2012 at 1:12am
Lovely! It works perfectly!
Thank you Wisely Done, and Peter 87, for your patience and diligence!
Topic archived. No new replies allowed.