0

I'm making a shop for my game, I opened a Fresh thing of code for this so this is the only code I'm running, I just want to get this to work before I put it into my actual game.
anyway what the problem is, is that no matter what input I give it always sends me to the inventory... here's the code:

# include <iostream>
# include <cmath>
# include <ctime>
# include <conio.h>

void invin();
int mana;
int health;
int maxhealth;
int str;
int def;
int gold;
int money = 600;
int healthpotlow = 0;
int healthpotmid = 0;
int healthpothigh = 0;
int healthpothuge = 0;
int healthpotuber = 0;
int buymana;
int buystr;
int buydef;
int buyhealth;
int choice;


using namespace std;



int main(){
    

    
    bool done = false;
    while (done != true){
    
    system("cls");
    cout << "Welcome to the Shop.\n\n"; 
    cout << "What Do you want to buy?\n\n";
    cout << "1. Low Health Potion:\n";
    cout << "On use Restores 10 Health. Costs 25 Gold.\n\n";
    cout << "2. Medium Health Potion:\n";
    cout << "On use Restores 20 Health. Costs 50 Gold.\n\n";
    cout << "3. High Health Potion:\n";
    cout << "On use Restores 30 Health. Costs 100 Gold.\n\n";
    cout << "4. Huge Health Potion:\n";
    cout << "On use Restores 40 Health. Costs 150 Gold.\n\n";
    cout << "5. Uber Health Potion:\n";
    cout << "On use Restores 50 Health. Costs 200 Gold.\n\n";
    cout << "6. 10 Health Points.\n\n";
    cout << "7. 1 Mana Point.\n\n";
    cout << "8. 1 Strength Point.\n\n";
    cout << "9. 1 Defense Point.\n\n";
    cout << "10. Go to your invintory.\n\n";
    cin >> choice;
    
           
           if (choice == 1 && gold >= 25){
                system("cls");
                cout << "You Have Bought a Low Health Potion for 25 Gold!\n\n";
                money = money - 25;
                cout << "You now have: " << money << " Gold.\n\n";
                healthpotlow = healthpotlow + 1;
                cout << "You now have: " << healthpotlow << " Low Health Potions.\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
           }
           
          else if (choice == 2 && gold >= 50){
                system("cls");
                cout << "You Have Bought a Medium Health Potion for 50 Gold!\n\n";
                money = money - 50;
                cout << "You now have: " << money << " Gold.\n\n";
                healthpotmid = healthpotmid + 1;
                cout << "You now have: " << healthpotmid << " Medium Health Potions.\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
                
           }
           
           else if (choice == 3 && gold >= 100){
                system("cls");
                cout << "You Have Bought a High Health Potion for 100 Gold!\n\n";
                money = money - 100;
                cout << "You now have: " << money << " Gold.\n\n";
                healthpothigh = healthpothigh + 1;
                cout << "You now have: " << healthpothigh << " High Health Potions.\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
                }
           
           
           else if (choice == 4 && gold >= 150){
                system("cls");
                cout << "You Have Bought a Huge Health Potion for 150 Gold!\n\n";
                money = money - 150;
                cout << "You now have: " << money << " Gold.\n\n";
                healthpothuge = healthpothuge + 1;
                cout << "You now have: " << healthpothuge << " Huge Health Potions.\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
             
           }
           
           else if (choice == 5 && gold >= 200){
                system("cls");
                cout << "You Have Bought a Uber Health Potion for 200 Gold!\n\n";
                money = money - 200;
                cout << "You now have: " << money << " Gold.\n\n";
                healthpotuber = healthpotuber + 1;
                cout << "You now have: " << healthpotuber << " Uber Health Potions.\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
            
           }
           
          else if (choice == 6 && gold >= 250){
                system("cls");
                maxhealth = maxhealth + 10;
                health = maxhealth;
                cout << "You Have Bought 10 Health Points!\n\n";
                cout << "Your total Health is now: " << maxhealth << "\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
              
           }
           
           else if (choice == 7 && gold >= 250){
                system("cls");
                mana = mana + 1;
                cout << "You Have Bought 1 Mana Point";
                cout << "Your total Mana is now: " << mana << "\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
           
           }
           
           else if (choice == 8 && gold >= 250){
                system("cls");
                str = str + 1;
                cout << "You Have Bought 1 Strength Point.";
                cout << "Your total Strength is now: " << str << "\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
         
           }
           
          else if (choice == 9 && gold >= 250){
                system("cls");
                def = def + 1;
                cout << "You Have Bought 1 Defense Point.";
                cout << "Your total Defense is now: " << def << "\n\n";
                cout << "Press Enter to Return to the Shop.\n\n";
                cin.ignore();
                cin.get();
            
           }
           
           else if (choice = 10){
                done = true;
                invin();
             
           }
           else {
                cout << "That is not a choice, Please try again.\n\n";
                cout << "Press Enter to Go to Continue.\n\n";
                cin.ignore();
                cin.get();
                }
}
    
}




void invin(){
    
    system("cls");
    
     
    

    
    cout << "Inventory:\n\n";
    cout << "Low Health Potions: " << healthpotlow << "\n\n";
    cout << "Medium Health Potions: " << healthpotmid << "\n\n";
    cout << "High Health Potions: " << healthpothigh << "\n\n";
    cout << "Huge Health Potions: " << healthpothuge << "\n\n";
    cout << "Uber Health Potions: " << healthpotuber << "\n\n";
    cout << "Press Enter to Return to the Main Menu.\n\n";
    cin.ignore();
    cin.get();
    main();
    
    }
3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Sodabread
1

Simple fix (you'll really kill yourself for this one ;) ). You have int gold; , which will set the value of gold to "0". Then, all of your options (apart from else and your inventory option) require you to have enough gold. Consider using a switch instead.

Set gold as 100. Then, have a switch. For example, if the user inputs option 1, you can test whether they have enough money, then go through the function if they do, and call a function or a line of code to tell them they don't have enough if they don't.
http://java-samples.com/showtutorial.php?tutorialid=327

0

i was going to use a switch statement then i changed my code and all that but i just realized the mistake... i set the money = to 600 not the gold... and in my regular code money is what i use for the actual money and gold is like the loot from a monster... anyway thanks for pointing out the gold thing

0

Looking at this code, you'll still always end up in the inventory because of this...

else if (choice = 10){
    done = true;
    invin();
}

You're assigning 10 to choice, rather than checking if it's equal. I couldn't tell you how many times I've made this same mistake =)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.