Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is a text-based RPG game I've made in C++ and would love some review on it. I am a 100% complete newbie with maybe 2 weeks of C++ experience so I'd love some lessons on what I did wrong and how I can improve! I mainly just use a lot of switch statements and if statements.

If there is anything wrong or things that could change and improve I'd very much appreciate if you could walk me through what was wrong, why it was wrong and how to prevent it from happening again. I really don't know a lot of C++ terms so ELIF would be awesome.

Here's my simple main.cpp. I think that's called a constructor and I'm using it to call my MeadowGiant.cpp down the road.

#include <iostream>
#include <string>
#include "MeadowGiant.h"

int main()
{
MeadowGiant obj1;
}

MeadowGiant.h

#define MEADOWGIANT_H

class MeadowGiant
{
public:
    MeadowGiant();
protected:
};

#endif`
#ifndef MEADOWGIANT_H
#define MEADOWGIANT_H

class MeadowGiant
{
public:
    MeadowGiant();
protected:
};

#endif

MeadowGiant.cpp

#include "MeadowGiant.h"
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

MeadowGiant::MeadowGiant()
{
int trail, caveEnter;
int fight1;
int loot;
int goblinAttack, goblinHP, HP, attack, action1, ability; 
goblinAttack = 5;
goblinHP = 10;
HP = 100;

std::cout << -Insert story narrative- Choose a trail to go down.\n";
std::cin  >> trail; 

    if(trail==1)
    {
    std::cout << "-Insert story narrative- Come across a cave. Do you go in?\n";
    std::cin  >> caveEnter;

        if(caveEnter==1)
        {
        std::cout << "You are ambushed by a goblin. Run or fight?\n";
        std::cin >> fight1;

        goblinFight:
        loot = rand() % 4 + 1;
        srand(time(NULL));           

        if(fight1==1)
        {
        std::cout   << "Your HP: " << HP << std::endl;
        std::cout   << "Enemy HP: " << goblinHP << std::endl;
        std::cout   << "What do you do?\n
                    << "[1] Attack [2] Run \n";
        std::cin    >> action1;
        }
            if(goblinHP<=0)
            {
            std::cout << "You have slain the goblin!\n" 
                      << "You head towards the chest and take the spoils\n"
                      << "of battle!\n";
                 switch(loot)
            {
            case 1: std::cout << "You find a bastard sword!\n"
                              attack = attack + 7;
                              goto exitCave;
            case 2: std::cout << "You find an Enchanted Staff!\n"
                              attack = attack + 10;
                              goto exitCave;
            case 3: std::cout << "You find an Obsidian Dagger!\n"
                              attack = attack + 9;
                              goto exitCave;
            case 4: std::cout << "You find a Holy Mace!\n"
                              attack = attack + 10;
                              goto exitCave;
            }
    else if(action1==1)
    {
    std::cout   << "You successfully hit the goblin!\n"
                << "He strikes back!\n";
    attack = rand() % 10 + 1;  
    srand(time(NULL));

    goblinHP = goblinHP - attack;
    HP = HP - goblinAttack;
    goto goblinFight;
    }
    else if(action==2)
    {
    std::cout   << "You take the cowards way out and leave the cave.\n";
    goto exitCave;
    }
    }
else if(caveEnter==2)
{
exitCave:
std::cout << "You have exited the cave.\n";
} 
else
{
goto exitCave;
}
}
}

The format of my actual code doesn't look like this in my IDE. The code is the same, but the tabs are much cleaner in my compiler so if that's an issue, I'm already on it.

share|improve this question
    
It appears you are missing a quotation mark here: std::cout << -Insert story narrative- Choose a trail to go down.\n";. Also, could you format your code so it appears the same in the question as it does in your IDE? We comment on formatting too, so it will help if we can comment on the right things. Additionally, I find spaces to be easier to manipulate than tabs, but that is personal preference. –  Hosch250 yesterday
    
I should probably start using spaces then, putting in my code exactly the way it is in my IDE with all those tabs is a nightmare. It looks like a small typo for that quotation mark. I'm more concerned on the 'syntax grammar' of my code. –  Adyn_G yesterday
    
I thought it was a small typo, or I would have had to close-vote it as not working. You should get some excellent reviews - there is a lot to comment on here (and that isn't a bad thing either). –  Hosch250 yesterday
    
I recommend that you take a book that teaches C++ and go through it (there is a neat list here stackoverflow.com/questions/388242/…). –  Alex M. 13 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.