-
functions
hello everyone (again)
for some strange reason, my compiler doesn't compile functions
of course, it's probably my mistakes
so i declared the function
int blah(int blah1, int blah2);
int main()
{
yeah yeah yeah;
blah(blah1, blah2);
return 0;
}
int blah(int blah1, int blah2)
{
the code;
return 0;
}
my code (like this one) will not compile.
the compiler says :
passing 'int' to argument 1 of 'blah(int *, int)' lacks a cast.
I did the functions according to tutorial, and i checked extra carefully so that there are no spelling errors.any help would be greatly appreciated
thanks
-
Please copy and paste your actual code into the message.
-
well... the code is far from being complete, and don't laugh at me! i'm a n00bie
//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[10], 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[10], userchoice);
return 0;
}
//Start game function
int startgame(int keepcounter[10], 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[10], userchoice);
}
else if (keepcounter[userchoice] == 0)
{
keepcounter[userchoice] = 1;
cout << "You have chosen to play at spot #" << userchoice << endl;
}
return 0;
}
-
Someone else will surely say this, so I will get it out of the way... USE [CODE] TAGS!
-
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
-
i'm still not quite getting it
about pointers and stuff..
so if i were to pass an array to a function, i should use a pointer instead
something like this?
int blah(int *pointer);
int main()
{
int x[10];
int *pointer;
pointer = &x;
blah(*pointer);
return 0;
}
int blah(int *pointer)
{
cout << *pointer;
return 0;
}
would this code work?
pointers are too confusing. -__-;;
-
THANKS SO MUCH! >_<
i understand it at last
i can't believe i didn't get something so simple =D