CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2008
    Posts
    43

    Returning Value Help

    I have the program tic-tac toe done and it works but there is one part that is messing up. In the function Game I have it set up so that it keeps playing until count >9. In the function Winners I have it set so that after someone wins it sets count=10, so that it ends the game. However when I run it doesnt recognize the count=10. I know I need to pass the count from the function Winners to the function Game. My question is how do I pass that?

    Enclosed is what I have.
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2004
    Posts
    296

    Re: Returning Value Help

    Try something like this:
    Code:
    #include <iostream>
    using namespace std;
    
    int Winners()
    {
    	int count = 10;
    	return count;
    }
    
    string Game(int status)
    {
    	if(status > 9)
    		return "game over";
    }
    
    
    int main() 
    {
    	int val = Winners();
    	string message = Game(val);
    	
    	cout<<message<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; April 4th, 2008 at 11:33 PM.

  3. #3
    Join Date
    Mar 2008
    Posts
    43

    Re: Returning Value Help

    Im not understanding the status and val part.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Returning Value Help

    You're passing count by value to the function Winners(). That means Winners only knows about a variable called count, which happens to have the same value as the variable called count in Game().

    The problem is getting a value you set in the Winners() function to somewhere that Game() will know about it. There are three primary options.

    1) All of your functions currently return void. You could make them return int instead, and assign the return value to the appropriate variable in Game(). This is the "simplest" approach.
    2) You could make the relevant variables global, rather than defining them in Game(). This is not recommended, as global variables are usually not good form. However, since you've already got some, one more won't hurt.
    3) You could pass the variable count to Winners() "by reference", rather than by value. In this case you really would be dealing with the same variable from Game(), not a copy. This is an advanced concept.

  5. #5
    Join Date
    Mar 2008
    Posts
    43

    Re: Returning Value Help

    Quote Originally Posted by Lindley
    You're passing count by value to the function Winners(). That means Winners only knows about a variable called count, which happens to have the same value as the variable called count in Game().

    The problem is getting a value you set in the Winners() function to somewhere that Game() will know about it. There are three primary options.

    1) All of your functions currently return void. You could make them return int instead, and assign the return value to the appropriate variable in Game(). This is the "simplest" approach.
    2) You could make the relevant variables global, rather than defining them in Game(). This is not recommended, as global variables are usually not good form. However, since you've already got some, one more won't hurt.
    3) You could pass the variable count to Winners() "by reference", rather than by value. In this case you really would be dealing with the same variable from Game(), not a copy. This is an advanced concept.
    Can you give me an example of the first option? Instead of void Winners() should it be int Winners() and then return count;

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Returning Value Help

    Well, logically, you only care about one aspect of the Winners() function in Game()----whether or not someone won. So perhaps it would be more appropriate to declare it as
    bool Winners()
    instead, and have return true; indicate a win, and return false; indicate otherwise.

    You could then modify Game() as such:
    Code:
    if (Winner(player1_wins, player2_wins, player1, player2, count) == true)
        count = 10;
    or, equivalently in this case,
    Code:
    if (Winner(player1_wins, player2_wins, player1, player2, count))
        break;

  7. #7
    Join Date
    Mar 2008
    Posts
    43

    Re: Returning Value Help

    Quote Originally Posted by Lindley
    Well, logically, you only care about one aspect of the Winners() function in Game()----whether or not someone won. So perhaps it would be more appropriate to declare it as
    bool Winners()
    instead, and have return true; indicate a win, and return false; indicate otherwise.

    You could then modify Game() as such:
    Code:
    if (Winner(player1_wins, player2_wins, player1, player2, count) == true)
        count = 10;
    or, equivalently in this case,
    Code:
    if (Winner(player1_wins, player2_wins, player1, player2, count))
        break;

    I tried using the bool Winners() and now I get errors. Ive enclosed the new program.
    Attached Files Attached Files

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Returning Value Help

    Quote Originally Posted by bananasplitkids
    I tried using the bool Winners() and now I get errors. Ive enclosed the new program.
    1) What are the errors?

    2) Read the FAQ that explains the use of debuggers and homework problems. Are you using one?

    http://www.codeguru.com/forum/showpo...46&postcount=2

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured