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.
Can't figure out what is wrong with my code in c++
What exactly is the problem or error you are getting?
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. ![]()
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?
You want to structure your code into functions. Far easier to debug and track down the problem.
This code does not compile, here is the log to help you :)
error C2360: initialization of 'currentgameState' is skipped by 'case' label
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.