Code:
//Program brought to you by: Derek Kwok
//Created on July 21, 2003
//Last Modified on July 21, 2003
//File name: ttt.cpp
#include <iostream.h>
int startgame(int *keepcounter, int userchoice);
int main()
{
int tic_tac_toe[3][3], userchoice, keepcounter[10], game, counter;
//Warm welcome message
cout << "Welcome to Derek's Mega Project #2 - Tic Tac Toe" << endl;
cout << "ENJOY!" << endl;
//The tic tac toe board
cout << endl;
cout << endl;
cout << " 1 | 2 | 3 " << endl;
cout << " --+---+--- " << endl;
cout << " 4 | 5 | 6 " << endl;
cout << " --+---+--- " << endl;
cout << " 7 | 8 | 9 " << endl;
//Reset of the keep counter
for (counter = 0; counter < 9; counter++)
{
keepcounter[counter] = 0;
}
startgame(keepcounter, userchoice);
return 0;
}
//Start game function
int startgame(int *keepcounter, int userchoice)
{
cout << endl;
cout << "Please enter the number where u would like to play at: ";
cin >> userchoice;
//check if that place is already played at
if (keepcounter[userchoice] == 1)
{
cout << "Sorry, you can't play there." << endl;
startgame(keepcounter, userchoice);
}
else if (keepcounter[userchoice] == 0)
{
keepcounter[userchoice] = 1;
cout << "You have chosen to play at spot #" << userchoice << endl;
}
return 0;
}
Try not to pass arrays into a function's parameter, instead see if you can use Pointers, and check the pointer's length.
"You can also pass an array to a function, but in this case the array is not copied, even though a pass-by-value method of passing arguments still applies. The array name is converted to a pointer, and a copy of the pointer to the beginning of the array is passed to the function. This is quite advantageous, as copying large arrays could be very time consuming." --C++ Tutorial from Wrox
also whenever you use arrays think of them as a chain of pointers. And that using the []s derefrences the pointer but leaving them out is like using the pointer without the *.
Which is why you cant call a function like
startgame(myarray[10],blah);
lets say the tenth number in myarray was 17
what your really doing is something like:
startgame(17,blah);
and your not telling the function where the array is.
But as a general note, try to avoid passing arrays into functions, it avoids confusion
-LP