Hello there. I am very new to C++ and still rather new to programming (Im only in high-school). I am trying to make a decision based game that runs in a cli to just get going with the language syntax somewhat. So far the game is working, and I made some functions that symplify game aspects (speech, decisions etc), but my code is very... amateur. I know that with programming there are lots of ways to tackle a problem, so Im looking for a slightly more elegant one.

The code I am posting isnt the actual game (with story etc) but should outline how my code works and what I am trying to accomplish. Along the way I will ask about various things that bother me. I feel like a real noob posting this but hey we all have to start somewhere.


Here are all the variables that I use, including an array for an inventory:

#include <cstdlib>
#include <iostream>
#include <time.h>

#define countof( array ) ( sizeof( array )/sizeof( array[0] ) )     //i didnt do this- i use
                                                                                  //resources that are at my disposal

using namespace std;

//RANDOM GAME VARIABLES
string strangerName = "The stranger";
string You = "You";


//THE STUFF IN YOUR BACKPACK
string inv[] = {"Unarmed", "A note", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
int invSize = countof(inv)-1;
int health = 20;
//--------------------------------------

OK I needed to know how large an array is, so I got that snippet off the net. I have no idea how it works, but I can still use it.

Now comes some functions that I made to simplify everything
I will only post the speech one for now:

//A FUNCTION THAT LETS PPL SAY STUFF
void speak(string person, string voice)
{
       cout << "\n" << person << ":   " << """" << voice << """" << "\n";
       sleep(1800);     
}

so now you could make someone say something by just typing:

speak(You, "I just said something");

Now I am reaching one of my first problems. For the inventory, I used a lot of loops to read how many Items you have, display them etc. Its REALLY messy:

//your inventory
void inventory()
{
     system("cls");
     
     string input;
     
    invSize = countof(inv);
     
     for (int j = 19; j > 0; j--){
         if (inv[j] == "")
         invSize--;
     }
     
     
     cout << "----------------------------------\n"
             "          Your Inventory          \n\n"; 
         
     for (int j = 0; j <= invSize; j++){
         if (inv[j] != "")
         cout << j+1 << ". " << inv[j] << "\n";
         }
         
     cout << "\nItems in inventory: " << invSize << "\n";
     
     cout << "----------------------------------\n";
     
     cout << "1. Close inventory";
     cin >> input;
     if (input != "some random input")
        system("EXIT");                       //   <-- This is 1 problem
}

As you can see, I use system("EXIT"); to get out of the inventory and back to where I was. However, when I get back to wherever I was (at a decision), the program just keeps running. This doesnt take you back to where you made the decision (and called inventory() from), so now I have to use labels to return to the decision, as shown below:

void game()
{
//this is just an example
string userInput;

speak(You, "Hello there. I have to make a decision");

cout << "1. Do this \n2. Do that";

decision:
cin >> userInput;

if (userInput == "1"){
          cout << "\nThis was done";
}else if (userInput == "2"){
         cout << "\nThat was done";
}else if (userInput == "#inv"){
         inventory();
         goto decision                        //    <-- this line is oh noes
}

/*
The next part of the story that you dont want to go to
*/

}

and the main function that shows a main menu and starts the game

int main(int argc, char *argv[])
{
//for purpose of this example
game();
system("PAUSE");
return 0;
}

So as you can see, after inventory() was called and exits, the program keeps running. The only solution that I have found so far is to use a label. But lots of decisions means lots of labels, which is a problem and makes coding tedious :(

Any idea what I can do to fix this problem and improve my code???

EDIT: Ok the edit button disappeared O_O so I'll post it here. I read the sticky so instead of

cin >> input;
if (input != "some random input")
system("EXIT");

I can just say getchar();

well thats 1 problem solved.

First of all, you should use classes. In addition to this you should use vectors instead of strings. And third, you should make your program platform inpedendent. This means not using system() .

What's wrong with this?

void game()
{
//this is just an example
string userInput;
 
speak(You, "Hello there. I have to make a decision");

do
{ 
    cout << "1. Do this \n2. Do that";
    cin >> userInput;
 
    if (userInput == "1") cout << "\nThis was done";
    else if (userInput == "2") cout << "\nThat was done";
    else if (userInput == "#inv") inventory();
} while (userInput != "q") 
}

hmm i thought of another way to make the code more readable. Instead of labels, make each deciding point a seperate function that has been declared as a prototype so that they can call back to each other. So instead of

Inventory()
{
//do this
return;
}
Goto decision;

You say

Inventory()
{
//do this
return;
}
decision();

As for the loop to keep displaying the decision (well thats how i understand it so far), thats pretty smart il try that when I get the time

thanks for the help man

Member Avatar for iamthwee

First of all, you should use classes. In addition to this you should use vectors instead of strings. And third, you should make your program platform inpedendent. This means not using system() .

What's wrong with this?

void game()
{
//this is just an example
string userInput;
 
speak(You, "Hello there. I have to make a decision");

do
{ 
    cout << "1. Do this \n2. Do that";
    cin >> userInput;
 
    if (userInput == "1") cout << "\nThis was done";
    else if (userInput == "2") cout << "\nThat was done";
    else if (userInput == "#inv") inventory();
} while (userInput != "q") 
}

You're missing a semi-colon at the end of the do-while clause.

ahh sorry.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.