Play my game !

this is the source code of a game i created.
it contains :

-snail races
-tic tac toe
-calculator
-random number generator

the code is simple i used functions to make it . hope you enjoy :)
kik : wu_lebanon.

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>

int menu (void);

char square[10] = {'o','1','2','3','4','5','6','7','8','9'};

using namespace std;

string y_o_n;
int placeBet;
int money = 200;
int winner2;
int winner;
int userChoice;
int userNumber;
int pcNumber = 0;
int numofguesses;
int usercalculator;
double usercalculatorinput1 , usercalculatorinput2 , sum;

double calculator(void);
void tictactoe (void);
string yesorno(void);
int init (void);
int rand_num(void);
int race (void);
int bet (void);

int main (void)
{
    cout<<" \t \t CODED BY : GILBERT SASSINE \n \n";
    cout<<"choose a number \n \n";
    menu();
    switch (userChoice)
    {
    case 1 :
         bet();
         race();
        break;
    case 2 :
        rand_num();
        break;
    case 3 :
        tictactoe();
    case 4 :
        calculator();
    }
    return 0;
}
int menu ()//menu function//////////////////////////////////////////////////////////////////
{   
    cout<<"1) race "<<endl
        <<"2) Number guessing game"<<endl
        <<"3) tic tac toe"<<endl
        <<"4) Calculator"<<endl
        <<"5) Quit "<<endl;

    cin>>userChoice;
    return userChoice;
}

int race ()//if the user chose 1/////////////////////////////////////////////////////////////////
{
    srand (time (0));
    winner = rand () % 3 + 1;

 system ("cls");
cout<<"welcome to the race! \n"
    <<"and the snails are off! \n";

Sleep (2000);


cout<<"and the winner is snail "<<winner<<"\n";

if (winner == winner2)
{
    cout<<"you won ! \n";
    money = money + placeBet;
    cout<<"balance : $"<<money<<"\n";
}
else
{
    cout<<"you lost \n";
    money = money - placeBet;
    cout<<"balance : $"<<money<<"\n";
}
yesorno();
return winner;
}
int bet ()//function that asks the user if he wants to continue or not///////////////
{
    cout<<"choose your snail number \n";
    cin>>winner2;
    cout<<"how to much do you want to bet on your snail? "<<endl;
    cout<<"you have $"<<money<<endl;
    cin>>placeBet;
    return winner2;
}
//if the user chose 2///////////////////////////////////////////////
int rand_num(void)
{
   init();
   cout<<"i have picked a number between 1 and 50"<<endl;
   for (numofguesses=0 ; pcNumber != userNumber ; numofguesses++)
{
cout<<"what would you like to guess? \n";
cin>>userNumber;

if(userNumber < pcNumber)//checks if the number picked is low

  cout<<"too low \n";

else if (userNumber > pcNumber)//checks if the number picked is high

  cout<<"too high \n";

}
cout<<"you guessed it! it took you "<<numofguesses<<" guesses \n";
yesorno();

return numofguesses;
}


int init ()//seed the generator and choose a random number///////////////////////////
{
    std::srand;
    std::srand;

srand (time(NULL));
pcNumber = rand () % 50 +1;
return pcNumber;
}
string yesorno()//functions that asks the user if he want to continue .i use it in the 3 choices in the menu.
{
    cout<<"do you want to continue? [y]es - [n]o"<<endl;
    cin>>y_o_n;

if (y_o_n == "y")
{
    Sleep (2000);
    system ("cls");
    main();
}
else if (y_o_n == "n")
{
    cout<<"press 3 :"<<endl;
    Sleep (2000);
    system ("cls");
    menu();
}
return y_o_n;
}
int checkwin()
{
	if (square[1] == square[2] && square[2] == square[3])

		return 1;
	else if (square[4] == square[5] && square[5] == square[6])

		return 1;
	else if (square[7] == square[8] && square[8] == square[9])

		return 1;
	else if (square[1] == square[4] && square[4] == square[7])

		return 1;
	else if (square[2] == square[5] && square[5] == square[8])

		return 1;
	else if (square[3] == square[6] && square[6] == square[9])

		return 1;
	else if (square[1] == square[5] && square[5] == square[9])

		return 1;
	else if (square[3] == square[5] && square[5] == square[7])

		return 1;
	else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
                    && square[4] != '4' && square[5] != '5' && square[6] != '6'
                  && square[7] != '7' && square[8] != '8' && square[9] != '9')

		return 0;
	else
		return -1;
}
void board()
{
	system("cls");
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

	cout << "     |     |     " << endl << endl;
}
void tictactoe (void)
{



int checkwin();
void board();

	int player = 1,i,choice;

	char mark;
	do
	{
		board();
		player=(player%2)?1:2;

		cout << "Player " << player << ", enter a number:  ";
		cin >> choice;

		mark=(player == 1) ? 'X' : 'O';

		if (choice == 1 && square[1] == '1')

			square[1] = mark;
		else if (choice == 2 && square[2] == '2')

			square[2] = mark;
		else if (choice == 3 && square[3] == '3')

			square[3] = mark;
		else if (choice == 4 && square[4] == '4')

			square[4] = mark;
		else if (choice == 5 && square[5] == '5')

			square[5] = mark;
		else if (choice == 6 && square[6] == '6')

			square[6] = mark;
		else if (choice == 7 && square[7] == '7')

			square[7] = mark;
		else if (choice == 8 && square[8] == '8')

			square[8] = mark;
		else if (choice == 9 && square[9] == '9')

			square[9] = mark;
		else
		{
			cout<<"Invalid move ";

			player--;
			cin.ignore();
			cin.get();
		}
		i=checkwin();

		player++;
	}while(i==-1);
	board();
	if(i==1)

		cout<<"==>\aPlayer "<<--player<<" win ";
	else
		cout<<"==>\aGame draw";
   Sleep (2000);
   system ("cls");
   menu();
	cin.ignore();
	cin.get();
}
/**************************************************************************
                    CALCULATOR FUNCTION
                    MULTIPLICATION - ADDITION
                        SUBTRACTION
***************************************************************************/

double calculator (void)
{
    system ("cls");

    cout<<"1) addition"<<endl
        <<"2) subtraction"<<endl
        <<"3) multiplication"<<endl;

    cin>>usercalculator;
    system ("cls");

if (usercalculator == 1)
{
   cout<<"enter 2  numbers: "<<endl;
    cout<<" \t";cin>>usercalculatorinput1;cout<<"\n \n";
    cout<<"+\t";cin>>usercalculatorinput2;cout<<"\n \n";

Sleep (1000);
system ("cls");
    sum = usercalculatorinput1 + usercalculatorinput2;
    cout<<" \t"<<usercalculatorinput1<<"\n \n"
        <<"+\t"<<usercalculatorinput2<<"\n \n"
        <<"____________________"<<"\n \n"
        <<" \t"<<sum<<"\n \n";
}
else if (usercalculator == 2)
{
cout<<"enter 2  numbers: "<<endl;
    cout<<" \t";cin>>usercalculatorinput1;cout<<"\n \n";
    cout<<"-\t";cin>>usercalculatorinput2;cout<<"\n \n";

Sleep (1000);
system ("cls");
    sum = usercalculatorinput1 - usercalculatorinput2;
    cout<<" \t"<<usercalculatorinput1<<"\n \n"
        <<"-\t"<<usercalculatorinput2<<"\n \n"
        <<"____________________"<<"\n \n"
        <<" \t"<<sum<<"\n \n";
}
else if (usercalculator == 3)
{
    cout<<"enter 2  numbers: "<<endl;
    cout<<" \t";cin>>usercalculatorinput1;cout<<"\n";
    cout<<"*\t";cin>>usercalculatorinput2;cout<<"\n";

Sleep (1000);
system ("cls");
    sum = usercalculatorinput1 * usercalculatorinput2;
    cout<<" \t"<<usercalculatorinput1<<"\n \n"
        <<"*\t"<<usercalculatorinput2<<"\n \n"
        <<"____________________"<<"\n \n"
        <<" \t"<<sum<<" \n \n";
}
}


Last edited on
errors and warnings

1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(67) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(131) : warning C4551: function call missing argument list
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(132) : warning C4551: function call missing argument list
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(134) : warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(141) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)


compiler: visual studio 2008
Last edited on

1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1144): could be 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1146): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,signed char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1148): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(1150): or 'std::basic_istream<_Elem,_Traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_Elem,_Traits> &,unsigned char &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(155): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_istream<_Elem,_Traits> &(__cdecl *)(std::basic_istream<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(161): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_ios<_Elem,_Traits> &(__cdecl *)(std::basic_ios<_Elem,_Traits> &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(168): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(175): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::_Bool &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(194): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(228): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned short &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(247): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(273): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned int &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(291): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(309): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__w64 unsigned long &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(329): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(__int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(348): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(unsigned __int64 &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(367): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(float &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(386): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(404): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(422): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio 9.0\vc\include\istream(441): or 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> while trying to match the argument list '(std::istream, std::string)'
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(143) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(const char [2], const char [2])'
1> c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(192): or 'int operator ==(const GUID &,const GUID &)'
1> while trying to match the argument list '(std::string, const char [2])'
1>d:\projects\visual studio\test_cpp\test_cpp\test_cpp.cpp(149) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(const char [2], const char [2])'
1> c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(192): or 'int operator ==(const GUID &,const GUID &)'
1> while trying to match the argument list '(std::string, const char [2])'
1>Build log was saved at "file://d:\Projects\Visual Studio\test_cpp\test_cpp\Debug\BuildLog.htm"
i use code::blocks and i didn 't get any error. just warning on some blank arguments and its legal
Line 147: Calling main() is illegal
\Test\main.cpp|147|error: ISO C++ forbids taking address of function '::main' [-Wpedantic]|

Your calculator function is not returning anything which is illegal. (And does not require diagnostic from the compiler).


If you are using C::B with MinGW, use these to make sure your compiler options adhere to standard:
In Settings → Compiler check:
Have g++ follow the C++11 ISO C++ language standard ( or Have g++ follow the coming C++0x ISO C++ language standard if you do not have that)
Enable warnings demanded by strict ISO C and ISO C++  
Treat as errors the warnings demanded by strict ISO C and ISO C++  
Enable all common compiler warnings (overrides many other settings)  
Enable extra compiler warnings  
Additional warnings would be fine too.
in my Orwell Dev C++ (TDM GCC 4.8.1, ISO C++11) there is no single warning or error. But in my Visual Studio 2012 there are 160 line errors!
in my Orwell Dev C++ (TDM GCC 4.8.1, ISO C++11) there is no single warning or error
Turn on warnings and use at least -Wall -Wextra -pedantic-errors
Topic archived. No new replies allowed.