Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I pass an array into a function without using pointers 1

Status
Not open for further replies.

ysablewolf

Programmer
Dec 10, 2004
2
CA
Hi I'm trying to pass an array into a function without using pointers..

My code is as follows


void Select_Game_Mode(void) ;
void Single_Player_Mode(void) ;
int Single_Player_Play(int bucket[], int arrSize) ;

int main()
{
srand(time(0)) ;

Select_Game_Mode() ;

return 0;

}

// Function to select game mode
// If the choices are not within the specified range then the program exits
void Select_Game_Mode()
{
cout << "Welcome to The Game of DICE BUCKET"
<< "\n\nPlease select the mode "
<< "\n\n1. Single player "
<< "\n2. Two Players "
<< "\n3. Play against the computer "
<< "\n\nEnter your choice : " ;
int playerChoice ;
cin >> playerChoice ;

if (playerChoice <= 0 || playerChoice > 3)
{
cerr << "Invalid Entry " ;
system("pause");
exit(0);
}
else
{
Print_Rules(playerChoice) ;
}

}

// Function for single player mode
void Single_Player_Mode(void)
{
char answer = 'y' ;
const int SIZE = 13;
bool arrBucket[SIZE] = {0} ;
int count (0), points(0), totalPoints(0), maxPoint(24); // 24 is the magic number of points the player needs equal or surpass in order to win

while (( answer == 'y' || answer == 'Y') )
{
points = Single_Player_Play ( (static_cast <int> arrBucket[]), SIZE) ; // function call
totalPoints = totalPoints + points ;

if (totalPoints >= maxPoint)
{
Check_Winner( totalPoints, maxPoint );
}

cout << "\nYour total winning so far is " << totalPoints
<< "\nDo you want to play another round ? You risk LOSING EVERYTHING! " ;
cin >> answer ;

if ( (answer == 'n' || answer == 'N') )
{
Check_Winner( totalPoints, maxPoint );
}



}
}


// singleplayer play
int Single_Player_Play(int bucket[], int arrSize)
{

int SinglePlayerPoints(0), round(0);

round = Dice_Roll();

for ( int i = 2; i <= arrSize; i++ ) // i is set to 2 because in a 2 die roll, the result can never be lower than 2
{
if ( round == i ) // check if dice roll matches the array element
{
if ( bucket ) // check and see if bucket is full
{
SinglePlayerPoints = 0 ;
cout << "\nSorry " << round << " has already been rolled"
<< "\nYou lose all your points" << endl ;
system("pause");
return 0 ;

}
else if ( !bucket ) // if bucket is empty, fill it
{
bucket = 1 ;
SinglePlayerPoints = SinglePlayerPoints + round ;
return SinglePlayerPoints ;
}
}
}


}


 
1. Use CODE tags for snippets (see Process TGML link on the message form).
2. It's a very strange problem: why do you need to pass bool array where yout function waits int array:
Code:
int Single_Player_Play(int bucket[], int arrSize) ;
...
bool arrBucket[SIZE] = {0} ;
...
points = Single_Player_Play 
((static_cast <int> arrBucket[]), SIZE); // Wrong statement...
Why do you want to cast this array (pointer) to int?!..
3. In that case use reinterpret_cast<int*>(), not a static_cast. But it's meaningless operation. You must declare this array as int[] then pass it to Single_Player_Play():
Code:
Single_Player_Play(array_name,size); // No [] brackets!..
4. Well, in C/C++ any occurence of an array name in executable statement implicitly converted to a pointer value(except sizeof op arg;). It's the language rule. Let's reread your original question now...
 
Code:
int Single_Player_Play(int bucket[], int arrSize);

bucket is a pointer, the same like in

Code:
int Single_Player_Play(int *bucket, int arrSize);
 
The array is a bool array...the int was mistake leftover from my previous code.

Thanks for the tips, The code's working fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top