954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Function question

I am once again getting confused with functions and arrays in C++.

I am trying to write a program that collects three values from a user, then displays those and does various things with them. In short, what's the problem with this line of code right here?

player = numPlayers(int[NUMPLAYERS]);


I am getting a parse error before '['. NUMPLAYERS is a const interger and numPlayers is a function that is being called.

Any help would be appreciated. Thank You!

Juggler
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

you have to give the arran a name

player = numPlayers(int MyArray[NUMPLAYERS]);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

you have to give the arran a name

player = numPlayers(int MyArray[NUMPLAYERS]);



Hmm. I tried that and I'm still getting the same error. "Parse Error before '['." Does something need to be done to the function that is being called in regards to the name given to the array? I gave that a try, and I'm not getting any errors with it, but it is a bit hard to determine if it's right since the program still won't compile since I'm getting the errors above.

bowler = bowlerNumber(int player[NUMPLAYERS]);

Heres the code that I asked about originally. I added a name to the array (I changed some names since posting this originally).

int bowlerNumber(int Player[NUMPLAYERS])
{
       int player;
       int i, bowlerNumber[NUMPLAYERS], total = 0;
       for (i = 0; i < NUMPLAYERS; i++)
           if (player < NUMPLAYERS)
              player = i;
       return player;
}

This is the function that is being called. Any ideas to why I'm getting this same parse error?

Juggler
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

you have to give the arran a name

player = numPlayers(int MyArray[NUMPLAYERS]);

Yuuc! that is totally wrong. If you are calling the function all you have to do is put the name of the array there, not the array size

player = numPlayers(MyArray);
if (player < NUMPLAYERS)

you have to add the substript to he array

if (player[i] < NUMPLAYERS)
          {
                player[i] = i;
          }
return player;


The above will return a pointer to the array. You declared the function returns an integer. Either change the function prototype/preemble or change the return value.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yuuc! that is totally wrong. If you are calling the function all you have to do is put the name of the array there, not the array size

player = numPlayers(MyArray);

you have to add the substript to he array

if (player[i] < NUMPLAYERS)
          {
                player[i] = i;
          }

you have to replaceMyArray with the name of your array that you created in your program. I only posted an example of how to call the function, and not intended for you to just paste into your program without change.

The above will return a pointer to the array. You declared the function returns an integer. Either change the function prototype/preemble or change the return value.



I think I see! What you posted before was what the function declaration should have looked like, rather than the code that was actually used to call the function.

But... By putting in the code this way, I get that in

player = numPlayers(MyArray);

MyArray is undeclared.

The function prototype is

int bowlerNumber(int MyArray[NUMPLAYERS]);


I'm still a bit confused.

Juggler
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

you have to replace MyArray with the name of the array you created in your program. I only posted an example of how to do it, and did not intend for you to just copy/paste it into your program without appropriate change.

you posted the function but you did not post main() or the function that calls bowlerNumber(), so I don't know the name of the array you need to pass.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi.. you need to declare the array first, then pass the array to numPlayers. I suggest reading resources such as Joel on Software to learn more about software development.

HelenHui
Newbie Poster
1 post since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You