How to say through in C++

Feb 24, 2012 at 2:27am
I was trying to do something using a varible such as experience or gold, and i was wondering if there was a way to set a limit to a greater than or less than statement like

1
2
3
4
5
6
7
8
9
10
if (gold//through statement that set the limit for gold)
{
    //make an option to buy something
}
else 
{
   cout << "You don't have enough money to buy this. \n";
   //I know when working with a store no limits is good but i want to be able to set a max
   return 1;
}


the problem with greater than or less than is that when using a loop, the test will always be true for the lack of the limit thanks for your time
Feb 24, 2012 at 2:47am
I don't know if I understand. Is this what you want?

1
2
3
4
5
6
7
8
9
10
11
if (gold >= cost)
{
    // do something
    gold -= cost;
}
else 
{
   cout << "You don't have enough money to buy this. \n";
   //I know when working with a store no limits is good but i want to be able to set a max
   return 1;
}
Feb 24, 2012 at 3:23am
no, i want something that says,

1
2
 while gold = 1-99 // do something
// but i dont want it to be subtraction, i want it to only work while the varible is between 2 numbers 


or mabye heres a better example
1
2
3
4
5
6
7
8
9
10
11
level=0
experience=0

if (experience 1-99//but not subtraction, while the number is between)
{
    level=1
}
if (experience 100-199//again not subtraction)
{
    level=2//and so on
}


does that help?
Feb 24, 2012 at 3:29am
1
2
3
4
5
6
7
8
if (experience >= 1 && experience <= 99)
{
    level=1;
}
if (experience >= 100 && experience <= 199)
{
    level=2;
}
Last edited on Feb 24, 2012 at 3:29am
Feb 24, 2012 at 3:32am
Okay! thanks so i just have to put a max and minimum, thanks soooo much
Feb 24, 2012 at 3:44am
If you make use of else if you can write it like this:
1
2
3
4
5
6
7
8
9
10
11
12
if (experience < 1)
{
    level=0;
}
else if (experience < 100)
{
    level=1;
}
else if (experience < 200)
{
    level=2;
}

It's probably easier to write it this, and less chance to make mistakes.
Feb 24, 2012 at 4:05am
that is easier, but i dont want it to continue to be level=0

because as you gain something like experience dont you want the varible to be 2 things at once, else if it causes the new statement to overwrite the old, dont you have to rerun it each time you gain experience
Topic archived. No new replies allowed.