Skip to main content
gamedev.net
🔒 Locked

Can't figure out what is wrong with my code in c++

Started by KiNgSGB May 25, 2013 at 2:32 AM 6 replies 1.8k views
Original Post
KiNgSGB
KiNgSGB

I've recently been learning c++ and I've been having trouble with game states. Here is my code. It is a text based rpg by the way. Not too complicated extremely basic.

http://codebin.org/view/06a657fb

Dragonsoulj
Dragonsoulj

What exactly is the problem or error you are getting?

KiNgSGB
KiNgSGB

Sorry I should have been more clear. The error is that once the user enters a input to choose whether or not to fight it just repeats that action. So over and over again no matter what the input is it just asks the user what they want to do.

DeafTV
DeafTV

The first problem is here:


gameState currentgameState = Fighting;

Since gameState is an enum, you don't need to use the type to refer to it again. Thus it should look like this.


switch (menuChoice)
{
case 1:
    currentgameState = Fighting; //Removed gameState here 
    break;

case 2:
    currentgameState = Fighting;  //And here.  I think you meant for this option to be statistics
    break;  //Needs another break here

default:
    break;  //Always good to have a default

}

I noticed a couple other places that also needed break statements. that's a common beginner error that can lead to some annoying bugs as well. Good to see someone familiarizing themselves with gamestates early on. smile.png

Fly Safe 7o
Khatharr
Khatharr

General Advice:

Separate your content (strings) from your code. In this case you may want to build a string table for long strings rather than using a long series of cout's at the start there. This will make it much easier to see the flow of your code. Also, please don't just try to cram everything into main. Split sections into their own functions.

Specific Advice:

Learn to use the debugger to follow the flow of your code. Are you familiar with the debugger for whatever it is that you're using?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Nicholas Kong
Nicholas Kong

You want to structure your code into functions. Far easier to debug and track down the problem.

jellyfishchris
jellyfishchris

This code does not compile, here is the log to help you :)

error C2360: initialization of 'currentgameState' is skipped by 'case' label

Dragonsoulj
Dragonsoulj

With full edits from DeafTV and suggestions, this does work, however there is not way to exit out of the loop, so you may want to add that.


#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
    bool gameIsRunning = true;
    enum gameState {Exploring, Fighting, Statistics};
    char name[100];
    int race;
    int level = 0;
 
    cout<<"\t\t Greenwood"<< endl;
    cout<<"Please enter you name: "<< endl;
    cin>>name;
    cout<<" "<< endl;
    cout<<" "<< endl;
    cout<<"Welcome, ";
    cout<<name<< endl;
 
    cout<<" "<< endl;
    cout<<" "<< endl;
    cout<<"Please select a race: "<< endl;
    cout<<"1. Human"<< endl;
    cout<<"2. Orc"<< endl;
    cout<<"Select: ";
    cin>>race;
 
    switch (race)
    {
    case 1:
        cout<<"You have chosen to be a human."<< endl;
        break;
    case 2:
        cout<<"You have chosen to be an orc."<< endl;
        break;
    default:
        cout<<"Invalid input, Please  select either 1 or 2."<< endl;
        break;
    }
 
    while (gameIsRunning)
    {
 
            int menuChoice;
            int fightChoice;
 
            gameState currentgameState = Exploring;
 
            if(currentgameState == Exploring)
            {
                cout<<"What would you like to do?"<< endl;
                cout<<"1.Fight"<< endl;
                cout<<"2.Show Stats"<< endl;
                cout<<"Select: "<<endl;
                cin>>menuChoice;
 
                switch (menuChoice)
                                {
                                case 1:
                                        currentgameState = Fighting; //Removed gameState here
                                        break;

                                case 2:
                                        currentgameState = Statistics;  //And here.  I think you meant for this option to be statistics
                                        break;  //Needs another break here

                                default:
                                        break;  //Always good to have a default

                                }
            }
             if(currentgameState == Fighting)
            {
                cout<<"You have been attacked by a goblin."<< endl;
                cout<<"1.Attack"<< endl;
                cout<<"2.Flee"<< endl;
                cin>>fightChoice;
 
                switch (fightChoice)
                {
                    case 1:
                        cout<<"You have killed the goblin."<< endl;
                        break;
                    case 2:
                        cout<<"You have died."<< endl;
                }
            }
             if(currentgameState == Statistics)
            {
                cout<<"You are level  ";
                cout<< level<< endl;
            }
 
    }
 
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.