Jump to content
  • Advertisement
Sign in to follow this  
KiNgSGB

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

This topic is 1848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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;
            }
 
    }
 
}
Edited by Dragonsoulj

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!