![]() |
RPG Code Issue
Another problem, I am trying to test to see if the player has enough gold to purchase items from the store before purchasing them, however it allows me to buy any item on the first input, no matter how much it costs, and no matter how much gold I have, it then puts me into a minus figure for gold. If you after the first purchase try to buy something else, it does test to see if you have enough gold, and if not it stops you from buying it.
Code:
Here is the complete file Code:
|
Re: RPG Code Issue
What's with all the asterisks? Makes the code very unreadable. Use spaces for indentation and code blocks when posting code here to preserve formatting.
Could you also give a screenshot (copy and paste the text, not a bitmap) of the input you give and output you get? In case 2, what is the value of mGoldRequired just before the if? |
Re: RPG Code Issue
Quote:
I think that is the issue, it does not know the value of mGoldRequired, but here is where it is confusing. Take the 2 seperate cpp files 1. Player.cpp 2. Store.cpp Now in the player code, I use spells which cost magicPoints, if I go to cast a spell, it tests to see if the player has enough magicPoints before casting the spell, if you dont have enough, then it will not let you cast that spell. Now the code in the store.cpp, is the same as the spell code, but what happens when you try to buy potions, is on the first pass it will let you buy something no matter how much gold you have, then put you into negative gold. Once you have purchased a potion, it will then not let you buy anything if you dont have enough gold. So why does it work on 1 piece of code and not the other. I have included both cpp files in my original post. In this example, I have from the onset 5 magic points and 10 gold. Spell: You encountered a goblin Prepare for battle 1) Attack, 2) Cast Spell, 3) Run Away: Please select your spell 1) FireBall 10 Magic Points 2) Water Jet 9 Magic Points 3) Lightening Bolt 8 Magic Points 4) Poison Wrap 7 Magic Points 1 You dont have enough Magic Points 1) Attack, 2) Cast Spell, 3) Run Away: Now potions: Welcome to my store, what can I do for you? 1) View Prices, 2) Buy Potions, 3) Buy Armor, 4) Rest a while, 5) Leave... 2 What Potions would you like to buy? We have 1) Belt, 2) Heal, 3) Poison, 4) Invisability... You have purchased a Heal Potion for 100 Gold 1) View Prices, 2) Buy Potions, 3) Buy Armor, 4) Rest a while, 5) Leave... Now if I try to buy it wont let me, as my gold is in the minus figures |
Re: RPG Code Issue
Try displaying the value, e.g.
Code:
cout << "You dont have enough gold for that (you have " << player.getGold() << " but need " << "mGoldRequired << ")" << endl; |
Re: RPG Code Issue
Quote:
You dont have enough gold for that you have -90 but need 100 It seems as if it is bypassing the if statement on its first iteration, strange as it works on the spell code just fine |
Re: RPG Code Issue
What *exact* type (a) does player.getGold() return, and (b) is mGoldRequired defined as? Specifically, are either of them unsigned?
It's not bypassing the if - it's doing it, we have to find out why it's evaluating false instead of true. |
Re: RPG Code Issue
In your swicth statement for the select variable I see something interesting.
Code:
******case 3:Code:
************mGoldRequired = 500;Code:
if(player.GetGold() > mGoldRequired) |
| All times are GMT +5.5. The time now is 07:07. |