Click here to Skip to main content
15,891,607 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: To bool or not to bool in C/C++ ? Pin
leon de boer21-Feb-20 11:55
leon de boer21-Feb-20 11:55 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Vaclav_22-Feb-20 5:34
Vaclav_22-Feb-20 5:34 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan22-Feb-20 6:47
mveRichard MacCutchan22-Feb-20 6:47 
GeneralRe: To bool or not to bool in C/C++ ? Pin
leon de boer23-Feb-20 5:29
leon de boer23-Feb-20 5:29 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Richard MacCutchan23-Feb-20 6:08
mveRichard MacCutchan23-Feb-20 6:08 
GeneralRe: To bool or not to bool in C/C++ ? Pin
Victor Nijegorodov23-Feb-20 20:43
Victor Nijegorodov23-Feb-20 20:43 
GeneralText based mining game using if and while Pin
Chleba22519-Feb-20 10:41
Chleba22519-Feb-20 10:41 
GeneralRe: Text based mining game using if and while Pin
Stefan_Lang19-Feb-20 21:42
Stefan_Lang19-Feb-20 21:42 
The forum isn't a very good place to post large chunks of code, as the code formatting is a bit harder to get right.

That said, I skimmed over the code, and without checking it in detail, there are already a few things I would advise:

1. Don't declare all variables up front. In C++, you should go by the idiom RAII (Resource acquisition is initialization - Wikipedia[^] ). That is, declare the variable when you need it, and immediately initialise it with a meaningful value.

2. Don't put everything into a single function, and most certainly not into main. main() should only contain the main control loop of the game, e. g. a simple loop that checks whether you want to continue the current game, start a new game, or exit. Continuing the game should call a function to process the next step or do whatever else your game does until the next check. Then you obviously need some kind of input and output; these should also go into separate functions. And then you need the functions that process your input and change the state of your game.

3. I see an incredible amount of 'else if' statements trying to deal with different input options, but no 'else' statements. This means that while you check for every option, you may fail to check for invalid input (more precisely: for things you didn't check in your if statements). Moreover, you're micxing checks on different variables which makes your program flow extremely hard to follow (and likely incorrect). It is much easier to structure and read your code if you use the switch() statement instead, and provide a default case to catch invalid input.

4. Games like these lend themselves very well to object-oriented programming. You have a character, a shop, a mine, and probably a few other things that can (and probably should) be implemented as a class You seem to be playing as a character who has some gear and can perforem some actions. Equipment comes in different forms, but always have a price and a name; this can be encoded in a class, or a set of classes. E. g.:
C++
class Equipment {
   int price_;
   string name_;
public:
   Equipment(int price, string const& name) : price_(price), name_(name) {} // simple constructor
   virtual ~Equipment() {} // this lets you use this class as a base class
   virtual int price() const { return price_; }
   virtual string name() const { return name_; }
};
class Helm : public Equipment {
public:
   Helm() : Equipment(250, "Helm") {} // simple constructor
   virtual ~Helm() {}
};
class Pickaxe : public Equipment {
   string type_;
public:
   Pickaxe(int price_, string const& name) : type_(name), Equipment(price, " pickaxe") {}
   virtual ~Pickaxe() {}
   string name() const { return type + " " + Equipment::name(); }
};
class Miner {
   int balance_;
   vector<Equipment*> items_;

   bool buy(Equipment const* item) {
      if (item->price() > balance_) {
         cout << "You only have " << balance_ << "$. You need "
              << item->price() << "$ to purchase a " << item->name() << endl;
         delete item;
         return false;
      } else {
         items_.push_back(item);
         balance_ -= item->price();
      }
      return true;
   }
public:
   ~Miner() {
      for (auto item_it : items_) {
         delete(*item_it);
      }
   }
   void equipHelm() { buy(new Helm()); }
   void equipPickaxe(int price, string const& type) { buy(new Pickaxe(price, type); }
};

Note how this design eliminates most of the repetitive parts in your code, and most functions are reduced to one-liners. Also, adding a new type of item now only requires a few lines of code: typically a simple class definition like the one for class Helm, and a new equip*() function in the Miner class.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)


modified 20-Feb-20 3:49am.

GeneralRe: Text based mining game using if and while Pin
Vaclav_20-Feb-20 17:04
Vaclav_20-Feb-20 17:04 
GeneralRe: Text based mining game using if and while Pin
Chleba22521-Feb-20 20:32
Chleba22521-Feb-20 20:32 
SuggestionRe: Text based mining game using if and while Pin
David Crow24-Feb-20 3:44
David Crow24-Feb-20 3:44 
QuestionCannot open file 'hid.lib' in microsip Pin
Jafar_Ulla19-Feb-20 2:14
professionalJafar_Ulla19-Feb-20 2:14 
AnswerRe: Cannot open file 'hid.lib' in microsip Pin
CPallini19-Feb-20 2:51
mveCPallini19-Feb-20 2:51 
QuestionUse of string class in c++ Pin
Awantika Singh14-Feb-20 21:26
Awantika Singh14-Feb-20 21:26 
AnswerRe: Use of string class in c++ Pin
Victor Nijegorodov14-Feb-20 21:35
Victor Nijegorodov14-Feb-20 21:35 
AnswerRe: Use of string class in c++ Pin
Stephane Capo14-Feb-20 21:38
professionalStephane Capo14-Feb-20 21:38 
GeneralRe: Use of string class in c++ Pin
Awantika Singh15-Feb-20 4:27
Awantika Singh15-Feb-20 4:27 
GeneralRe: Use of string class in c++ Pin
Richard MacCutchan15-Feb-20 5:06
mveRichard MacCutchan15-Feb-20 5:06 
GeneralRe: Use of string class in c++ Pin
Stephane Capo15-Feb-20 21:52
professionalStephane Capo15-Feb-20 21:52 
GeneralRe: Use of string class in c++ Pin
k505416-Feb-20 3:30
mvek505416-Feb-20 3:30 
GeneralRe: Use of string class in c++ Pin
Richard MacCutchan16-Feb-20 4:58
mveRichard MacCutchan16-Feb-20 4:58 
GeneralRe: Use of string class in c++ Pin
CPallini16-Feb-20 7:28
mveCPallini16-Feb-20 7:28 
GeneralRe: Use of string class in c++ Pin
Stefan_Lang16-Feb-20 22:43
Stefan_Lang16-Feb-20 22:43 
GeneralRe: Use of string class in c++ Pin
CPallini16-Feb-20 23:02
mveCPallini16-Feb-20 23:02 
QuestionVfW Saving Compression Options Pin
Member 1305023114-Feb-20 5:35
Member 1305023114-Feb-20 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.