Ask coding questions

← Back to all posts
C++ warning in function
LoveFromSkyy (130)

Good morning!

My function consists of 2 global variables coins and XP


In case you are wondering what this function does, I'll explain.


The function is made for after the player wins the level. When this happens, they gain XP and coins. For some reason, the program is giving me a warning on the return value XP


The function:

// If player wins the Level
int LvlWin(int XP, int coins) {
	XP += 100;
	coins += 100;
	return XP, coins;
}

How can I fix this?
The warning is at return XP, coins;


An extra favor?

I don't want my game to only drop a certain amount of coins or give only 100 XP every single level. How can I change this to be random between a certain amount of numbers?

(if you answer both, i will select your explanation as the answer to this question)
;)
Thank you

I am not including the entire program due to other possible errors that i may be fixing while awaiting the answer to this question

Answered by xxpertHacker (860) [earned 5 cycles]
View Answer
Comments
hotnewtop
xxpertHacker (860)

Oh, I missed the

How can I change this to be random between a certain amount of numbers?

for that, you might be interested in std::random:

There's a lot to random number generation, so it can quickly get complicated if you attempt to dive in, so I'd recommend honestly, just ignoring it, and picking a generator.

Here ya go: https://en.cppreference.com/w/cpp/numeric/random/random_device

Check the example at the end, or the quick one I put up here: https://replit.com/@xxpertHacker/random-example.

xxpertHacker (860)

Currently, you are using C++'s comma operator, which is... practicially useless.

(x, y) evaluates to y, therefore, (XP, coins) == coins; you're just returning coins, not XP.

Also, XP and coins are only mutated within the scope of the function, maybe you wanted to pass by reference?

I'd suggest using a std::pair to return XP and coins at once.

Example:

// If player wins the Level
auto LvlWin(int const experience, int const coins) {
	return std::make_pair(experience + 100, coins + 100);
}

void foo() {
    Player player1 = { ... };

    auto const [ new_experience, new_coins ] = LvlWin(player1.experience, player1.coins);
}

But more likely, you'd want a global player struct, and you'd mutate experience and coins as globals instead.

LoveFromSkyy (130)

there's an auto function type?
can you tell me more about it?
@xxpertHacker

xxpertHacker (860)

@LoveFromSkyy auto is one C++'s type deduction keywords.

signed int x = 0;
auto y = x; // y is the type of x, therefore y is a "signed int"

I just didn't want to type out std::pair<int, int>; but that's the type that the std::make_pair return type is deduced as.

As for the auto [ experience, coins ] = ..., the autois needed.

Some references:
https://en.cppreference.com/w/cpp/keyword/auto
https://en.cppreference.com/w/cpp/language/auto
https://en.cppreference.com/w/cpp/language/structured_binding

LoveFromSkyy (130)

Wow, you're good at C++!
Honestly, thank you @xxpertHacker
Have a wonderful day!
is it ok if i ping you if i get confused later on about something?

LoveFromSkyy (130)

When I am executing this, would I need to change this:

	// If player wins
		if (levelMonster.monsterHealth <= 0) {
			
			ClearScreen();
			cout << "You win!\n";
			sleep(3);
			ClearScreen();
		LvlWin(XP, coins);

			GameMenu();
		}

That is for if the player wins.
@xxpertHacker

xxpertHacker (860)

@LoveFromSkyy
You're welcome, good luck.

is it ok if i ping you if i get confused later on about something?

Umm... I might be lat to respond, and there's people more experienced in C++ here, but I already have people who ping me, so sure, feel free.

xxpertHacker (860)

@LoveFromSkyy

// If player wins
  if (levelMonster.monsterHealth <= 0) {
    ClearScreen();
    cout << "You win!\n";
    sleep(3);
    ClearScreen();
    LvlWin(XP, coins);
    GameMenu();
}

The return value of LvlWin is unused, so I think you wanted to pass by reference instead?

void LvlWin(int& exp, int& coins) {
    exp += 100;
    coins += 100;
}

???

AntarcticaAdv (31)

It is not possible to return multiple values, you can wrap the elements in a tuple if you want to return multiple values, like this:

#include <iostream>
#include <tuple>

std::tuple<int, int> LvlWin(int XP, int coins) {
  XP += 100;
  coins += 100;
  return std::make_tuple(XP, coins);
}

int main() {
  auto result = LvlWin(10, 10);
  std::cout << "XP: " << std::get<0>(result) << std::endl;
  std::cout << "coins: " << std::get<1>(result) << std::endl;
}

Here are the docs if you want to read more.

Edit: sorry for the lack of time, I have to get going and don't have time for the other question.

Coder100 (17045)

also here's a headache for ya:
https://en.cppreference.com/w/cpp/utility/tuple

it's the 'python-to-cpp' answer
(if you know what i mean)

Coder100 (17045)

You cannot return two things.
You should do something like this:

struct LvlWin { int XP; int coins; };
LvlWin data;

void lvl_win(int& xp, int& coins) {
	xp += 100;
	coins += 100;
	return LvlWin { xp, coins };
}

sorry I'm dumb

Also your functions should always be lower case bruh
Also you can't assign to a copy, that won't work, assign by reference.

Coder100 (17045)

owo how do you know it's morning for me
you wizard

RhinoRunner (807)

well, functions should be declared with void and not int

void LvlWin(int XP, int coins) {
	XP += 100;
	coins += 100;
	return XP, coins;
}
LoveFromSkyy (130)

I though void types dont have return values though
@RhinoRunner

RhinoRunner (807)

@LoveFromSkyy ah my bad, i dont use C++ too often

LoveFromSkyy (130)

Its fine, thanks though!
Nice to see a fellow programmer tryna help out the best they can :)
us coders must stick together, y'[email protected]