Public Group

# my first rpg

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

## Recommended Posts

i have started my first rpg but now i'm stuck maybe someone can help me please the problem is when you pick the option to go to the inn it dosent instead it leaves town and then it gose to the inn after you pic the option for exploring...i am wrighting in c++ and my compiler is dev_c++ here is the code
#include <iostream>
using namespace std;

int main()

{
//intro and picking a name
cout <<"\twelcome to my first game!\n";
cout <<"enter user name: ";
cout<<"1 - human\n";
cout<<"2 - orc\n";
cout<<"3 - elf\n";
cout<<"4 - dwarf\n";
cout<<"5 - gnome\n";
int pickRace;
cin >>pickRace;

switch (pickRace)

{
case 1:
cout <<"you picked the human race.\n";
break;
case 2:
cout <<"you picked the orc race.\n";
break;
case 3:
cout <<"you picked the elf race.\n";
break;
case 4:
cout <<"you picked the dwarf race.\n";
break;
case 5:
cout <<"you picked the gnome race.\n";
break;
default:
cout <<"error - invalid input; only 1,2,3,4, or 5 allowed.\n";
}

int charclass;
cout <<"\nPick your charicter class: \n";
cout <<"1 - Fighter\n";
cout <<"2 - Mage\n";
cout <<"3 - Warlock\n";
cout <<"4 - Priest\n";

cout <<"Pick your charicter class: ";
cin >>charclass;

switch (charclass)
{
case 1:
cout <<"You picked Fighter.\n\n";
break;
case 2:
cout <<"You picked Mage.\n\n";
break;
case 3:
cout <<"You picked Warlock.\n\n";
break;
case 4:
cout <<"You picked Priest.\n\n";
break;
default:
cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";
}

/*Now the game begins...This is the first town you start in and where you
can go in the town... This is where the problem is when you pick the option to go to the inn it dosent it leaves town instead and then it gose to the inn after you pic the option for exploring*/

int town1;
cout <<"\nwhat would you like to do: \n";
cout <<"1 - leave town\n";
cout <<"2 - go to inn\n";
cout <<"3 - go to blacksmith\n";
cout <<"4 - go to shop\n";

cout <<"what would u like to do: ";
cin >>town1;

switch (town1)
{
case 1:
cout <<"u picked leave town.\n\n";
break;
case 2:
cout <<"You picked go to inn.\n\n";
break;
case 3:
cout <<"You picked go to blacksmith.\n\n";
break;
case 4:
cout <<"You picked shop.\n\n";
break;
default:
cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";
}

//option 1 leaving the town and exploring the surrondings
//and the directions you can go

int leavetown;
cout <<"\nwhere would you like to go: \n";
cout <<"1 -north\n";
cout <<"2 -east\n";
cout <<"3 -south\n";
cout <<"4 -west\n";

cout <<"where would you like to go: ";
cin >>leavetown;

switch (leavetown)
{
case 1:
cout <<"\nyou picked to go north\n";
break;
case 2:
cout <<"\nyou picked to go east\n";
break;
case 3:
cout <<"\nyou picked to go south\n";
break;
case 4:
cout <<"\nyou picked to go west\n";
break;
default:
cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";
}

//this is option 2 the inn and what you can do here

int inn;
cout <<"\nwelcome to the inn. What would you like to do: \n";
cout <<"1 - rest\n";
cout <<"2 - eat\n";
cout <<"4 - leave\n";

cout <<"welcome to the inn. What would you like to do: ";
cin >>inn;

switch (inn)
{
case 1:
cout <<"\nrest\n";
break;
case 2:
cout <<"\neat\n";
break;
case 3:
break;
case 4:
cout <<"\nleave\n";
break;
default:
cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";
}

system("PAUSE");
return 1;
}


so what am i doing wrong...any advice is helpfull. [Edited by - Emmanuel Deloget on August 13, 2007 9:40:53 AM]

##### Share on other sites
Well, what's wrong with it? I didn't read the code, but I'm not sure what to look for. Is it compiling fine? Is it getting stuck in an infinite loop? We need to know why you're stuck to help get you unstuck.

##### Share on other sites
First, you don't say what your problem is.

Second, from what I understand, Dev-C++ is no longer supported, so pick up a more modern IDE like Code::Blocks or MSVS2005C++EE if you want a free one.

Third, I'm not a C++ programmer.

Fourth, use source tags to enclose your source code. I'll go ahead and do it for future posters trying to respond.

#include <iostream>using namespace std;int main(){  //intro and picking a name  cout <<"\twelcome to my first game!\n";  char username [100];  cout <<"enter user name: ";  cin >>username;  cout <<"Hello, "<<username<<"!\n\n";  //picking your race  cout<<"please pic your race: \n";  cout<<"1 - human\n";  cout<<"2 - orc\n";  cout<<"3 - elf\n";  cout<<"4 - dwarf\n";  cout<<"5 - gnome\n";  int pickRace;  cout<<"pick your race: ";  cin >>pickRace;    switch (pickRace)    {    case 1:      cout <<"you picked the human race.\n";      break;    case 2:      cout <<"you picked the orc race.\n";      break;    case 3:      cout <<"you picked the elf race.\n";      break;    case 4:      cout <<"you picked the dwarf race.\n";      break;   case 5:      cout <<"you picked the gnome race.\n";      break;   default:      cout <<"error - invalid input; only 1,2,3,4, or 5 allowed.\n";   }  //picking your charicters class  int charclass;  cout <<"\nPick your charicter class: \n";  cout <<"1 - Fighter\n";  cout <<"2 - Mage\n";  cout <<"3 - Warlock\n";  cout <<"4 - Priest\n";    cout <<"Pick your charicter class: ";  cin >>charclass;    switch (charclass)  {    case 1:      cout <<"You picked Fighter.\n\n";      break;    case 2:      cout <<"You picked Mage.\n\n";      break;    case 3:      cout <<"You picked Warlock.\n\n";      break;    case 4:      cout <<"You picked Priest.\n\n";      break;    default:      cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";   }/*Now the game begins...This is the first town you start in and where youcan go in the town... This is where the problem is when you pick the option to go to the inn it dosent it leaves town instead and then it gose to the inn after you pic the option for exploring*/  int town1;  cout <<"\nwhat would you like to do: \n";  cout <<"1 - leave town\n";  cout <<"2 - go to inn\n";  cout <<"3 - go to blacksmith\n";  cout <<"4 - go to shop\n";   cout <<"what would u like to do: ";  cin >>town1;  switch (town1)  {    case 1:      cout <<"u picked leave town.\n\n";      break;    case 2:      cout <<"You picked go to inn.\n\n";      break;    case 3:      cout <<"You picked go to blacksmith.\n\n";      break;    case 4:      cout <<"You picked shop.\n\n";      break;    default:      cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";  }  //option 1 leaving the town and exploring the surrondings  //and the directions you can go   int leavetown;  cout <<"\nwhere would you like to go: \n";  cout <<"1 -north\n";  cout <<"2 -east\n";  cout <<"3 -south\n";  cout <<"4 -west\n";  cout <<"where would you like to go: ";  cin >>leavetown;  switch (leavetown)  {    case 1:      cout <<"\nyou picked to go north\n";      break;    case 2:      cout <<"\nyou picked to go east\n";      break;    case 3:      cout <<"\nyou picked to go south\n";      break;    case 4:      cout <<"\nyou picked to go west\n";      break;    default:      cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";  }  //this is option 2 the inn and what you can do here  int inn;  cout <<"\nwelcome to the inn. What would you like to do: \n";  cout <<"1 - rest\n";  cout <<"2 - eat\n";  cout <<"3 - buy food\n";  cout <<"4 - leave\n";  cout <<"welcome to the inn. What would you like to do: ";  cin >>inn;  switch (inn)  {    case 1:      cout <<"\nrest\n";      break;    case 2:      cout <<"\neat\n";      break;    case 3:      cout <<"\nbuy food\n";      break;    case 4:      cout <<"\nleave\n";      break;    default:      cout <<"Error - Invalid input; only 1,2,3 or 4 allowed.\n";  }  system("PAUSE");  return 1;}

Good luck.

Another thing I noticed is that you're very inconsistent. Sometimes you declare the integer that will hold the user's choice before the switch statement, and sometimes you declare it after the switch. There are other things, too, but that's just an example. Consistency = good.

[/edit]
[edit 2]

After reading your code, I saw where you described the problem. In the future, you should describe the problem before you post the source.

Now, the reason you exit the town before you go to the inn is because you're not using anything to tell the program that it needs to go to the inn. You just tell the user he goes to the inn. You need to write a function or something that makes the user go to the inn. Not sure if C++ has it, but AVOID THE GOTO STATEMENT. It has its (rare) uses, but it's generally the devil.

Your best bet is to totally refactor your code and arrange it into classes et al. That's the benefit of OO languages like C++.

You should also use loops.
[/edit]

##### Share on other sites
What I am having problems grasping is how you go from picking a direction(Inn, Leave Town, Blacksmith, etc.) to the actual actions themselves? It would appear that the switch statements run in a row, so that LeaveTown will always be first and Inn will always be second and so forth.

Maybe you should use an if statement, for example (pseudo-code):

Switch Statement
CHOICE ONE: Leave Town
CHOICE TWO: Inn
cin userschoice

if userschoice == 1
LeaveTown()
elif userschoice == 2
Inn()
else
'No silly, you can't go to' userschoice

##### Share on other sites
ok thanks & how do i do code tags.

##### Share on other sites
Type [ DONT WRITE THIS source lang="cpp"] CODE GOES IN HERE
[/source DONT WRITE THIS] But leave out the DONT WRITE THIS

##### Share on other sites
Code tags: the word 'code' enclosed in square brackets.

Source tags: the word 'source' enclosed in square brackets.

Both without the single quote marks, obviously.

##### Share on other sites
Quote:
 Original post by GallivanMaybe you should use an if statement, for example (pseudo-code):Switch StatementCHOICE ONE: Leave TownCHOICE TWO: Inncin userschoiceif userschoice == 1 LeaveTown()elif userschoice == 2 Inn()else 'No silly, you can't go to' userschoice

so where would i put it in the code...i just stared to learning this stuff on friday so bare with me for a bit

##### Share on other sites
Ideally you'll probably want your game to look like this:

# Functions go here, this is where you describe all your locations and what will
# happen if you call them.

Town()
cout Welcome to the town.
cout etc.

Inn()
cout Welcome to the inn.
cout etc.

# You should also define a menu here. This will allow you to revert back to the

switch
choices

# Then you'll want to start your game. Up to now you've just defined things
# which won't actually show up unless you call them. To start your game you
# probably want an intro and then your menu to come up.

cout Welcome to my game. I hope you have fun!

# This (besides for stating variables) is pretty much it. Once you call your
# menu it will go up to where you defined what your menu should do and run that.
# From inside the menu you'll run the if statement, where if user choice == 1
# then Town() or Inn() or whatever.

--

In short:

Town()
Inn()
Woodlands()
Forest()

cout introduction

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 10
• 11
• 37
• 12
• 12
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999958
×