-1
#include<iostream>
#include<cstdlib>
#include<string>
#include<time.h>
using namespace std;

//Functions
// player strategy
int strategy(int user1Strat, int user2Strat);
// player total score per round
int currentScore();
// Display game result
void printResults();

int main()
{
    int total_player1 = 0; // player 1 current score
    int total_player2 = 0; // player 2 current score
    int player1_strat= 0;  //player 1 strategy for each turn
    int player2_strat = 0; // player 2 strategy for each turn

    // seed the random number generator.
    srand(static_cast<int> (time(NULL)));

    // get strategy for each player using functions <strategy>

    strategy(player1_strat, player2_strat);

    cout << player1_strat << endl << player2_strat << endl;

    system("pause");
    return 0;
}

int strategy(int user1Strat, int user2Strat)
{
    int x,
        y;

    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
    x = user1Strat;
    y = user2Strat;

    return x, y;
}

While calling for function strategy in the function main it will execute how it should, but once I ask to return the value it will just return

Enter player1's roll until strategy: 10
Enter player2's roll until strategy: 5

0
0

press any key to contiue...

Does anyone know why this is happening or what is causing it, was my error in the strategy function? Or upon calling it?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
willi hdz
  • 13
  • 1
  • 3
  • That's not how you return multiple values in C++ from a function. Look up "returning by reference". – Bob May 02 '16 at 05:13
  • Your understanding of returning values from functions is flawed. Please read a textbook on the subject. – R Sahu May 02 '16 at 05:13
  • 4
    Also you don't take any return value from the function anyway. X and y don't magically appear on some random variables you would like. A basics tutorial/book will help you here. – Sami Kuhmonen May 02 '16 at 05:20
  • The `strategy` function should return a `std::pair`, not a single `int`. – David Schwartz May 02 '16 at 06:06
  • Possible duplicate of [Returning two variables in a C++ function](http://stackoverflow.com/questions/15365860/returning-two-variables-in-a-c-function) – dandan78 May 02 '16 at 06:08

2 Answers2

1

strategy(player1_strat, player2_strat); in your main() do nothing after receiving inputs so you won't see any change on player1_strat and player2_strat.

If you want to modify player1_strat and player2_strat in strategy, you could do that by referencing:

void strategy(int& user1Strat, int& user2Strat)
{
    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
}

or you could return "multiple value" by using std::pair:

//#include <utility>
std::pair<int, int> strategy(int user1Strat, int user2Strat)
{
    int x, y;

    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
    x = user1Strat;
    y = user2Strat;

    return std::make_pair(x, y);
}

//main()
std::pair<int, int> result = strategy(player1_strat, player2_strat);

x = result.first;
y = result.second;
Rahn
  • 4,787
  • 4
  • 31
  • 57
0

You can only return a single object from a function. return x, y; does not return x and y but only y. If you want to update multiple variables, give them to the function as references and change their value inside the function.

Edit:

As mentioned by @Keith Thompson in the comments, the comma actually is an operator in this statement. It evaluates x (not much to do there), discards the result and then evaluates and returns the second argument y.

Alexander Büse
  • 564
  • 7
  • 15
  • 1
    *`return x, y;` is not allowed.* It is syntactically valid. It doesn't do what the OP is expecting. – R Sahu May 02 '16 at 05:15
  • @RSahu You are right that was not precise. I'll edit my answer. – Alexander Büse May 02 '16 at 05:16
  • eh, would "return x, y" return x and treat "y" as another expression by itself? – Einheri May 02 '16 at 06:10
  • 1
    You should mention the comma operator. – Keith Thompson May 02 '16 at 06:21
  • @KeithThompson True. Updated my answer. Thanks for your comment. – Alexander Büse May 02 '16 at 06:26
  • 1
    @user3109672: `return x, y;` is equivalent to `return(x, y);` — that is, `x` is evaluated and the result discarded, then `y` is evaluated and the value of `y` is returned. Normally, the evaluation of `x` will have some side-effect; here is doesn't, and a compiler would probably warn about that (at least if warnings and/or optimization is enabled). – Jonathan Leffler May 02 '16 at 06:41