Returning multiple values

I was thinking of putting in functions for enemy encounters. Something like:

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
//enemy.cpp
int encounter1 (int hp, int ammo)
{
//...
//...
//...
return hp;
return ammo;
}

//main.cpp
//...
int main ()
{
//These are going to be assigned values later
int hp, ammo;
//...
cout << "Do you want to fight? (Y/N)" << endl;
cin >> choice;

if (choice == 'Y')
{
encounter1 (hp, ammo);
cout << "HP remaining : " 
 << hp
  << endl;
cout << "Ammo remaining: "
 << ammo
  << endl;
}
else
{
return 0;
}


But hp and ammo stay same, as initialized in main.cpp. I deduce that they aren't returning properly, or idk how to return multiple variables. Help please.
You can't return multiple values like that, a way to get around it is to use std::pair And return it - http://www.cplusplus.com/reference/utility/pair/pair/

Or if you want to return 3 stuff, use a std::tuple - http://en.cppreference.com/w/cpp/utility/tuple

Edit: definitely recommend using what @naaissus recommends in the post below me :D
Last edited on
Also he can maybe use references
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
(there is a section Returning multiple values via out parameters)
@Tarik
Thanks! I am looking forward to use those :D

@naaissus
I will try to implement those too. Thanks!
Topic archived. No new replies allowed.