gamecreator function does wierd stuff

hi,

I have made a game generator whitch links wo people from a database to play a game but for some reason it decided to not work. it compiles fine but when executed you get a kind of cin action and no matter what the program does not go on. the most wierd part is that i do not hava any cin statements in that function.

basicly how I intended the function to work is like this:

1. take 2 random numbers between 0 and 100 (100 is the max amount of players possibile.)

2. check if they aren't the same number (if so generate a new number)

3. check if one of the name strings is empty (this part failed before, then I edited a bit making the error written before)

4. launch game

function :
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
void game(){
	char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2, i = 0;
	bool check = false, check2 = false ;
	while (check == false){	
		i1 = rand() % 100;
		i2 = rand() % 100;
		while (check2 == false) 
			if (i1 == i2){
				i2 = (i2 + rand() % 100) - 100;
			} else {
				if (Abbo[i1].name.empty() == 0 || Abbo[i2].name.empty() == 0){
					check2 = false;
				} else {
					check2 = true;
					check = true;
				}
			}
		}
	while (i < 20){
			name1[i] = Abbo[i1].name[i];
			name2[i] = Abbo[i2].name[i];
			surname1[i] = Abbo[i1].surname[i];
			surname2[i] = Abbo[i2].surname[i];
			
			i++;
	}
	Abbo[i1].games++;
	Abbo[i2].games++;
	cout<<"       player 1:     player 2: \n";
	cout<<"game: "<<name1<<surname1<<" VS :"<<name2<<surname2<<endl;
	play(i1,i2);
}


function after editing:
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
void game(int i){    \\ i is number of players
char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2,j = 0;
	bool check = false, check2 = false ;
	while (check == false){	
		i1 = rand() % 100;
		i2 = rand() % 100;
		while (check2 == false) 
			if (i1 == i2){
				i2 = (i2 + rand() % 100) - 100;
			} else {
				if (i1 > i || i2 > i){
					check2 = false;
				} else {
					check2 = true;
					check = true;
				}
			}
		}
	while (i < 20){
			name1[j] = Abbo[i1].name[j];
			name2[j] = Abbo[i2].name[j];
			surname1[j] = Abbo[i1].surname[j];
			surname2[j] = Abbo[i2].surname[j];
			
			j++;
	}
	Abbo[i1].games++;
	Abbo[i2].games++;
	cout<<"       player 1:     player 2: \n";
	cout<<"game: "<<name1<<surname1<<" VS :"<<name2<<surname2<<endl;
	play(i1,i2);
}


total code:
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <time.h>

using namespace std;

struct RecAbbo{
		std::string name;
		std::string surname; 
		int games;
		int won;
		int same;
		int lost;
		int money;
	}Abbo[100];
	
void play(int pl1, int pl2);
void game(int i);
int add(int i);

int main(){
	srand (time(NULL));
	int i,choise;
	bool exit = false;
	while (!exit){
		cout<<"1: add player  \n";
		cout<<"2: play a game  \n";
		cout<<"0: exit  \n";
		cout<<"choose: ";
		cin>>choise;
		switch (choise){
			case 1:
				i = add(i);
				break;
			case 2:
				game(i);
				break;
			case 3:
				exit = true;
				break;
			default:
				cout<<"please rechoose \n";
		}
	}
}
int add(int i){
	cout<<"name: ";
	cin>>Abbo[i].name;
	cout<<"surname: ";
	cin>>Abbo[i].surname;
	Abbo[i].games = 0;
	Abbo[i].won = 0;
	Abbo[i].same = 0;
	Abbo[i].lost = 0;
	Abbo[i].money = 0;
	i++;
	return i;
	}
void game(int i){
char name1[20],name2[20],surname1[20],surname2[20];
	int i1, i2,j = 0;
	bool check = false, check2 = false ;
	while (check == false){	
		i1 = rand() % 100;
		i2 = rand() % 100;
		while (check2 == false) 
			if (i1 == i2){
				i2 = (i2 + rand() % 100) - 100;
			} else {
				if (i1 > i || i2 > i){
					check2 = false;
				} else {
					check2 = true;
					check = true;
				}
			}
		}
	while (i < 20){
			name1[j] = Abbo[i1].name[j];
			name2[j] = Abbo[i2].name[j];
			surname1[j] = Abbo[i1].surname[j];
			surname2[j] = Abbo[i2].surname[j];
			
			j++;
	}
	Abbo[i1].games++;
	Abbo[i2].games++;
	cout<<"       player 1:     player 2: \n";
	cout<<"game: "<<name1<<surname1<<" VS :"<<name2<<surname2<<endl;
	play(i1,i2);
}
void play(int pl1, int pl2){
int i, p1,p2,mode,p1money = 50, p2money = 50;
	srand (time(NULL));
	bool x = true,z = true;
	while (x){
	cout<<"for easy mode press 1 "<<endl;
	cout<<"for normal mode press 2 "<<endl;
	cout<<"for hard mode press 3 "<<endl;
	cout<<"for beast mode press 4 "<<endl;
	cout<<"choise: ";
	cin>>mode;
	switch (mode)
	{
		case 1:
			i = rand() %100;
			cout<<"number between 1 and 100"<<endl;
			x=false;
			break;
		case 2:
			i = rand() %500;
			cout<<"number between 1 and 500"<<endl;
			x=false;
			break;
		case 3:
			i = rand() %2500;
			cout<<"number between 1 and 2500"<<endl;
			x=false;
			break;
		case 4:
			i = rand() %10000;
			cout<<"number between 1 and 10000"<<endl;
			x=false;
			break;
		default:
			cout<<"please choose again"<<endl;
			break;
	}
	while(z){
		cout<<"player 1: ";
		cin>>p1;
		cout<<"player 2: ";
		cin>>p2;
		if(p1 < i)
		cout<<"number player 1 is higher"<<endl;
		if(p1 > i)
		cout<<"number player 1 is lower"<<endl;
		if(p2 < i)
		cout<<"number player 2 is higher"<<endl;
		if(p2 > i)
		cout<<"number player 2 is lower"<<endl;
		if (p1 == i){
			cout<<"player 1 has won!!"<<endl;
			Abbo[pl1].won++;
			Abbo[pl2].lost++;
			Abbo[pl1].money = Abbo[pl1].money + p1money;
			cout<<"money won for player 1: "<<p1money<<endl;
			z = false;
			} else {
			p1money=p1money-5;
		}
		if (p2 == i){
			cout<<"player 2 has won!!"<<endl;
			Abbo[pl2].won++;
			Abbo[pl1].lost++;
			Abbo[pl2].money = Abbo[pl2].money + p2money;
			cout<<"money won for player 2: "<<p2money<<endl;
			z = false;
			} else {
			p2money=p2money-5;
		}
		if (p1money == 0 || p2money == 0){
			Abbo[pl1].same++;
			Abbo[pl2].same++;	
			}
		}
	}
}	

I think the loop on line 8 may loop forever under certain circumstances.
@helios in the first or the second version?

for the first
I was thinking that as well but the game gets loaded after a while. the only thing is that it does go on to the second function (the game) which is after the cicle has ended
Last edited on
Topic archived. No new replies allowed.