• Create Account

# My First Text Based RPG help

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1KiNgSGB  Members   -  Reputation: 114

Like
0Likes
Like

Posted 15 May 2013 - 07:26 PM

When I say this it is not a very good game at all. I actually have a problem that I need some input on. I know the error but I just don't know how to fix it. Here is the entire code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
char name[100];
cout<<"\t\tGreenwood"<< endl;
cout<<"Welcome stranger, what is your name?:"<< endl;
cin>>name;
cout<<"Welcome "<<name<<"\n";

cout<<"1. Human \n";
cout<<"2. Orc \n";
int pickRace;
cin>>pickRace;

switch(pickRace)
{
case 1:
cout<<"You have chosen to be a human.\n";
break;
case 2:
cout<<"You have chosen to be an orc.\n";
break;
default:
cout<<"Invalid input, you many only select 1 or 2/.\n";
}

int difficulty;
cout<<"Select a difficulty: "<< endl;
cout<<"1.Easy"<< endl;
cout<<"2.Medium"<< endl;
cout<<"3.Hard"<< endl;

cin>>difficulty;

switch(difficulty)
{
case 1:
cout<<"You have picked Easy."<< endl;
break;
case 2:
cout<<"You have picked Medium."<< endl;
break;
case 3:
cout<<"You have picked Hard."<< endl;
break;
default:
cout<<"Invalid input."<< endl;
break;
}

int choice;
int attack = 2;
int cHealth;
int eHealth = 8;
int yn;
int stats;
int looper = 7;

while (looper > 1)
{
cout<<"Select a command: "<< endl;
cout<<"1.Fight"<< endl;
cout<<"2.Stats"<< endl;
cout<<"Select: ";
cin>>choice;

switch (choice)
{
case 1:
cout<<"Attacked by a Goblin! Attack back?"<< endl;
cout<<"1.Yes"<< endl;
cout<<"2.No"<< endl;
cin>>yn;
switch (yn)
{
case 1:
while (eHealth > 0)
{
eHealth - attack;
if (eHealth <= 0)
{
cout<<"You have killed the goblin."<< endl;
cout<< endl;
++stats;
break;
}
}

case 2:
cout<<"You have died."<< endl;
break;
}
break;

case 2:
cout<<"Level "<<stats<< endl;
cout<<" "<< endl;
cout<<" "<< endl;
break;

}

}
}

The problem is at:

cout<<"Select a command: "<< endl;
cout<<"1.Fight"<< endl;
cout<<"2.Stats"<< endl;
cout<<"Select: ";
cin>>choice;

switch (choice)
{
case 1:
cout<<"Attacked by a Goblin! Attack back?"<< endl;
cout<<"1.Yes"<< endl;
cout<<"2.No"<< endl;
cin>>yn;
switch (yn)
{
case 1:
while (eHealth > 0)
{
eHealth - attack;
if (eHealth <= 0)
{
cout<<"You have killed the goblin."<< endl;
cout<< endl;
++stats;
break;
}
}

case 2:
cout<<"You have died."<< endl;
break;
}
break;

case 2:
cout<<"Level "<<stats<< endl;
cout<<" "<< endl;
cout<<" "<< endl;
break;

}

If I take out the part with while (eHealth > 0). I can get it to work fine, but I would like a health system. I need some help with the health system. By the way I'm also pretty new to programming so I may not understand some terms.

Edited by KiNgSGB, 15 May 2013 - 07:39 PM.

### #2Rafael_Treiber  Members   -  Reputation: 166

Like
1Likes
Like

Posted 15 May 2013 - 08:04 PM

I am not sure on your problem but one thing that caught my attention was the line

 eHealth - attack;


Did you mean...

eHealth -= attack;


I believe this is your problem.

### #3KiNgSGB  Members   -  Reputation: 114

Like
0Likes
Like

Posted 15 May 2013 - 08:12 PM

Ok thanks but now the problem is the the switch goes through case 1 and 2. So instead of just killing the goblin and returning to the beginning, it also says you have died. How would I fix that.

### #4Servant of the Lord  Crossbones+   -  Reputation: 12807

Like
0Likes
Like

Posted 15 May 2013 - 08:20 PM

What happens when your code reaches this block of code?

while (eHealth > 0)
{
eHealth - attack;
if (eHealth <= 0)
{
cout<<"You have killed the goblin."<< endl;
cout<< endl;
++stats;
break;
}
}

Think of it in isolation a second. Once your execution enters into the while() statement, it is stuck there until you let it leave.

As far as the flow of logic goes, just have one main loop, and branch the flow of logic by having different "states" your game is in.

while(gameIsRunning)
{
if(gameState == IN_COMBAT)
{
//Do something...
}
else if(gameState == EXPLORING)
{
//Do something else...
}
else if(gameState == IN_SHOP)
{
//Do something different...
}
}

I'm not saying only have one loop period, but that conceptually you should have one main loop of logic, and not let execution get "stuck" in different loops elsewhere (there are reasons to have separate logic loops, but rarely).

Edited by Servant of the Lord, 15 May 2013 - 08:21 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #5KiNgSGB  Members   -  Reputation: 114

Like
0Likes
Like

Posted 15 May 2013 - 08:31 PM

Ok so I understand what your saying there, but how do i declare each thing (gameIsRunning, IN_SHOP, EXPLORING, IN_SHOP). Sorry if it's a very noob question, but I'm still learning.

### #6warnexus  Prime Members   -  Reputation: 924

Like
0Likes
Like

Posted 15 May 2013 - 09:10 PM

Since gameIsRunning is inside a condition, what type of variable do you think it is?

You should learn how to define a function and call a function immediately. Functions are used to contain your code so your program has better structure.

You should also have better variable for the health variables. cHealth and eHealth and especially yn is a bad variable name. Make general variable names.

Edited by warnexus, 15 May 2013 - 09:14 PM.

### #7jellyfishchris  Members   -  Reputation: 300

Like
2Likes
Like

Posted 15 May 2013 - 10:55 PM

For those of us that cant read code formatted like that:

http://codebin.org/view/94bf6d09

Ok I am confused what you actually what the program to do so I am just going to stab in the dark.

while (eHealth > 0)
{
eHealth - attack;

if (eHealth <= 0)
{
cout<< "You have killed the goblin."<< endl;
cout<< endl;
++stats;
break;
}
}

In your loop you are doing eHealth - attack.

So lets think about this, your doing the golbins health minus attack which is 1. Now you never store the 1 and it is just forgotten about. Now ehealth is greater than 0 because its still 8 because it is never saved. Now the program will go into an infinite loop.

However lets fix this and do eHealth = eHealth - attack;

First loop:

eHealth = 1;

do if statement = no

Second loop:

eHealth = -6;

do if statement = yes

output "You killed the goblin \n"

break while loop;

Additionally stats was never assigned so you will get a runtime failure, so if you change it to int stats = 0;

Ok your next question is going to be it comes up with but it also printed "You have died" this is becuase you have forgotten to add a break statement for the case of attacking the goblin. So it will execute case1 which you kill the goblin, it never finds a break to break the case so it does case2 also then case2 finds a break and finishs.

Hope this is enough information and good luck!

### #8Chad Smith  Members   -  Reputation: 1026

Like
0Likes
Like

Posted 16 May 2013 - 01:21 AM

When using the advanced editor (BBCODE Mode turned off) their is a GUI button that looks like this: <>.  The code button.  You can put your code in that so your code will be formatted.

If just using BBCODE, You can just do the code tags.

### #9Rafael_Treiber  Members   -  Reputation: 166

Like
0Likes
Like

Posted 16 May 2013 - 12:49 PM

I hope that by now you should have figured out what your problem is but just to add a few comments, you would greatly improve your code if you learned functions, as Servant of the Lord said. Using that structure you can find where your problem is much easily.

Also, learn how to use the debugger from your compiler to see the values of your variables and to see why you are not getting the results that you expect.

And lastly, as Chad Smith also said, use the code tags to post your code here. It makes the code much more readable for us here. I also don't know if you used indentation on your code or not but it is another tip to make it more readable.

Don't be frightened if you are just starting to code. It seems a lot to grasp just to make a text based rpg but if you follow these tips you will notice how much faster you will find errors in your code.

### #10Servant of the Lord  Crossbones+   -  Reputation: 12807

Like
0Likes
Like

Posted 16 May 2013 - 02:18 PM

Ok so I understand what your saying there, but how do i declare each thing (gameIsRunning, IN_SHOP, EXPLORING, IN_SHOP). Sorry if it's a very noob question, but I'm still learning.

The same way you declare your other variables:

bool gameIsRunning = true;

You can use an enum for states:

enum GameState {Exploring, Fighting, Shopping};

GameState currentGameState = Exploring;

if(currentGameState == Exploring)
{
//...
}

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #11KiNgSGB  Members   -  Reputation: 114

Like
0Likes
Like

Posted 16 May 2013 - 08:23 PM

Thanks for all the help guys. I'm starting to understand a bit more about c++. Need some work on functions but I'll get there eventually.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS