6

How do i add minimum and maximum values for an integer? I want an integer to never go down below zero like negative and never goes above 100

Here is the example:

int hp = 100;
std::cout << "You cast healing magic to yourself!" << std::endl;
hp += 20;
mp -= 25;

For example the health is 100 but when a healing magic is cast it became 120. The thing i want is i want it to stay as 100 no matter how many healing magic are cast upon.

asked Jun 6, 2022 at 8:47
2
  • 4
    You can't do this with any normal integer type. You could create a type, but it probably isn't worth it. I'd suggest looking at calling std::clamp at times when you update the value. Commented Jun 6, 2022 at 8:51
  • In this case (where you know the direction of the change) a simple std::min(hp + 20, 100) would be enough. The mana you should check beforehand and not allow the spell to succeed, if the character has not enough. Commented Jun 6, 2022 at 9:19

3 Answers 3

15

You can use std::clamp:

hp = std::clamp(hp + 20, 0, 100);
mp = std::clamp(mp - 25, 0, 100);
answered Jun 6, 2022 at 8:52
Sign up to request clarification or add additional context in comments.

1 Comment

using min and max may save you a couple instruction :)
4

You can use std::clamp as suggested by @TedLyngmo if you are using a compiler which supports C++ 17. If not, then you can write a simple function to manage the limits for hp and mp:

void change(int& orig, int val)
{
 int temp = orig + val;
 if (temp <= 0)
 temp = 0;
 else if (temp >= 100)
 temp = 100;
 orig = temp;
}
int main()
{
 int hp = 40, mp = 40;
 std::cout << "You cast healing magic to yourself!" << std::endl;
 
 change(hp, 50);
 change(mp, -25);
 std::cout << hp << " " << mp << std::endl;
}
answered Jun 6, 2022 at 9:47

Comments

-2

I believe what you are saying is whatever the healing magic is you want to display 100 or your hp the way it is. If so you can store the 20 and 25 as variables and create another var with the same value as ur original one and play around with that. Don't change the value of ur original one and so you get to display that.

answered Jun 6, 2022 at 8:56

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This especially helps, if you would revert lots of changes, if it does not succeed. It would make sense to bundle those variables in a class or struct and make a copy of all of them like a transaction). This case probably is simpler.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.