-3

This is a stupid question, but honestly I can't get it to work in my program. I just started C++ and I keep doing it wrong. I have the user enter in the value of 'pile' and then i want to go over to my second function and divide pile by two. My professor says I'm not allowed to use global variables. Here's my code:

int playerTurn();
int main() //first function
{
    int pile = 0;
    while ( pile < 10 || pile > 100 ) {
        cout << "How many marbles would you like there to be?" << endl;
        cout << "Please choose between 10 and 100: ";
        cin >> pile;
    }
    return pile; //the variable I'm trying to return is pile
    playerTurn();
}

int playerTurn(pile) //second function
{
    int limit = pile / 2; //says pile is an undeclared identifier
}

I can't seem to get the 'pile' over to my other function, playerTurn

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
Average kid
  • 6,885
  • 6
  • 21
  • 17

4 Answers4

1

The return statement quits a function and returns a value to where it is being called.

What your code doing is thus quitting main() and giving pile back to the operating system.

You need to call playerTurn, using pile as argument.

speeder
  • 6,197
  • 5
  • 34
  • 51
1

The return statement returns immediately from the current function. So when you use it in the main function it returns from the main function.

To pass a variable to another function you pass it as an argument:

playerTurn(pile);

Also, when you declare a function taking arguments, you have to fully specify the arguments, just like you do other variables:

void playerTurn(int pile)
{
    // ... your implementation here...
}

If you having trouble understanding passing arguments or returning values, then you should continue reading the basics until you understand it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
  • Your forward definition of playerTurn doesn't match the implementation. You need to change this to int playerTurn(int pile).

  • Your implementation of playerTurn doesn't specify argument types (i.e. int).

  • As far as I can see, you're trying to return pile from main. This will actually exit your program. You seem to instead want to pass this as an argument. To do this, just put it inside the brackets instead (and of course, get rid of the return xyz; line).

slugonamission
  • 9,562
  • 1
  • 34
  • 41
0

Check comments for description

int playerTurn(); // Function declaration

int main() //first function
{
    int pile; // Define variable before usage
    do  // Google do-while loops.
    {        
        cout << "How many marbles would you like there to be?" << endl;
        cout << "Please choose between 10 and 100: ";
        cin >> pile;
    }while ( pile < 10 || pile > 100 );

    // Calling the secondary function and passing it a parameter 
    // and then getting the result and storing it in pile again.
    pile = playerTurn(pile) 
}

// Not really sure what you are trying to do here but 
// assuming that you want to pass pile to this function 
// and then get the return type in main

int playerTurn(int pile) 
{
    int limit = pile / 2; //Processing.
    return limit;         // Return from inside the function, this value goes to pile in main()
}
Abijeet Patro
  • 2,842
  • 4
  • 37
  • 64
  • thanks it worked! although in the first line, i had to put an 'int pile' into int playerTurn(), so it was int playerTurn(int pile) – Average kid Feb 22 '13 at 21:00
  • Yeah sorry about that. The function prototype should have atleast the type for the variables...so this would have worked too. `int playerTurn(int ) // No variable needed in function declaration` Also like the others I suggest that you get a good book to help you through your class. – Abijeet Patro Feb 23 '13 at 05:18