is it possible to return 2 values from a function

for my functtion twoPlayers() I want to return the winning player and the winning score to main. I was wondering if I could do an if/else with one returning one winner and one returning the other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//CornHole_Program

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int twoPlayers();
int fourPlayers();

const int CORNHOLE = 3;
const int WOODY = 1;
const int FOUL = 0;

int main()
{
    char choice;
	cout << "Hello! would you like to play singles or doubles?\n";
	cout << "For Singles, please press 'A', for Doubles, please press 'B'\n";
	
	cin >> choice;
	
	while (choice != 'A' && choice != 'B')
	{
        cout << "Invalid selection, Please enter 'A for singles and 'B' for Doubles.\n";
        cout << "Your Choice: ";
        cin >> choice;
	}
	
	if (choice == 'A')
	{
	    cout << "Singles, Have fun!\n";
	    twoPlayers();
	}
	else
	{
	    cout << "Doubles, Have fun!\n";
	    fourPlayers();
	}
}


int twoPlayers()
{
    string playerOne, playerTwo;
    int roundNumber = 0;
    int playerOneTotalScore = 0;
    int playerTwoTotalScore = 0;
    

    cout << " \n" << " \n" << "______________SINGLES______________\n" << endl;
    cout << "Please enter the name for player one and player two.\n";
    
    cout << "Player One: ";
    cin.ignore();
    getline(cin, playerOne);
    cout << "Player Two: "; 
    getline(cin, playerTwo);
    
    cout << " \n" << playerOne << ", you will be going first." << endl;
    
    while (playerOneTotalScore < 21 && playerTwoTotalScore < 21)
    {
        int playerOneRoundScore = 0, playerTwoRoundScore = 0;
        int p1CornHoleNum = 0, p1WoodyNum = 0, p1FoulNum = 0,
        p2CornHoleNum = 0, p2WoodyNum = 0, p2FoulNum = 0;
        
        //Display the Current Round.
        roundNumber++;
        cout << " \n" << " \n" << " \n" << " \n"
        << "______________" << "ROUND " << roundNumber << "______________"
        << " \n" << " \n" << " \n" << endl;
        
        
        
        
        cout << playerOne << " How many cornholes did you score?" << endl;
        cin >> p1CornHoleNum;
        playerOneRoundScore += (CORNHOLE * p1CornHoleNum);
        cout << playerOne << " How many woodys did you score?" << endl;
        cin >> p1WoodyNum;
        playerOneRoundScore += (WOODY * p1WoodyNum);
        cout << playerOne << " How many of your shots were foul shots?" << endl;
        cin >> p1FoulNum;
        playerOneRoundScore += (FOUL * p1FoulNum);
        
        cout << " \n" << playerTwo << " How many cornholes did you score?" << endl;
        cin >> p2CornHoleNum;
        playerTwoRoundScore += (CORNHOLE * p2CornHoleNum);
        cout << playerTwo << " How many woodys did you score?" << endl;
        cin >> p2WoodyNum;
        playerTwoRoundScore += (WOODY * p2WoodyNum);
        cout << playerTwo << " How many of your shots were foul shots?" << endl;
        cin >> p2FoulNum;
        playerTwoRoundScore += (FOUL * p2FoulNum);
        
        if (playerOneRoundScore > playerTwoRoundScore)
        {
            cout << playerOne << " scored " << (playerOneRoundScore - playerTwoRoundScore)
            << " points this round." << endl;
            playerOneTotalScore += playerOneRoundScore;
        }
        else if (playerTwoRoundScore > playerOneRoundScore)
        {
            cout << playerTwo << " scored " << (playerTwoRoundScore - playerOneRoundScore)
            << " points this round." << endl;
            playerTwoTotalScore += playerTwoRoundScore;
        }
        else
            cout << "Nobody scored this round" << " \n" << " \n" << endl;
    }
    
    return 0;
}






int fourPlayers()
{
    cout << "Working on it" << endl;
    
    return 0;
}
yes, a lot of ways...
you can return a container (vector, list, array, whatever) and know that 1st is winner 2nd is loser or whatever scheme
you can return a tuple<>
you can return an object (struct, class)
you can use reference parameters and overwrite their values
and probably other ways too.
tuple/container is probably the best generic approach.
tbh i have no idea what any of those are, can I simply have an
if()
return x
else
return y?
yes, but that returns only one value. it just chooses which one.
if that is all you need, its fine.
A branch would only return 1 thing, just at different times. You might still use a branch in your solution, but it's not the solution.

I want to return the winning player and the winning score to main.

You should either return a struct which contains the information you want, or pass the data by reference from main. This is what jonnin is talking about.

Method 1: Using a struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Example program
#include <iostream>
#include <string>
using namespace std;

struct Winner {
    string name;
    int points;
};

Winner play_game()
{
    // ... playing game ...
    int player1_points = 42;
    int player2_points = 36;
    string player1 = "Bob";
    string player2 = "Charlie";
    
    if (player1_points > player2_points)
    {
        return Winner { player1, player1_points };   
    }
    else if (player2_points > player1_points)
    {
        return Winner { player2, player2_points };   
    }
    else // TODO: How do you want to handle a draw?
    {
        return Winner { "nobody", 0 };
    }
}

int main()
{
    Winner winner = play_game();
    
    cout << winner.name << " won";
    if (winner.points > 0)
    {
        cout << " with " << winner.points << " points!\n";   
    }
    else
    {
        cout << ".\n";
    }
}


Method 2: Using reference parameters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Example program
#include <iostream>
#include <string>
using namespace std;

void play_game(string& winner_name, int& winner_points)
{
    // ... playing game ...
    int player1_points = 42;
    int player2_points = 36;
    string player1 = "Bob";
    string player2 = "Charlie";
    
    if (player1_points > player2_points)
    {
        winner_name = player1;
        winner_points = player1_points;
    }
    else if (player2_points > player1_points)
    {
        winner_name = player2;
        winner_points = player2_points;
    }
    else // TODO: How do you want to handle a draw?
    {
        winner_name = "nobody";
        winner_points = 0;
    }
}

int main()
{
    string winner;
    int points;
    play_game(winner, points);
    
    cout << winner << " won";
    if (points > 0)
    {
        cout << " with " << points << " points!\n";   
    }
    else
    {
        cout << ".\n";
    }
}



Last edited on
@Ganado, WOW! thats alot of help!
I am super new at the language and dont know what a struct is,
also in this scenario there cant be a draw because only one player can score per round.

My basic question was answered and I thank you all for the help.
As only 2 values are to be returned, there's also std::pair as well as std::tuple.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Example program
#include <iostream>
#include <string>
#include <utility>

using Result = std::pair<std::string, int>;

Result play_game() {
	// ... playing game ...
	int player1_points = 42;
	int player2_points = 36;
	std::string player1 = "Bob";
	std::string player2 = "Charlie";

	if (player1_points > player2_points) {
		return {player1, player1_points};
	} else if (player2_points > player1_points) {
		return {player2, player2_points};
	} else // TODO: How do you want to handle a draw?
	{
		return {"nobody", 0};
	}
}

int main() {
	const auto [name, pnts] {play_game()};

	std::cout << name << " won";

	if (pnts > 0) {
		std::cout << " with " << pnts << " points!\n";
	} else {
		std::cout << ".\n";
	}
}


Topic archived. No new replies allowed.