Help with random text adventure game c++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
richred20
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Nov 11, 2010 6:41 am

Help with random text adventure game c++

Post by richred20 »

I wanted to see what i can make what i could make with c++ however i did not want to make a game design so i just based it off of the book eragon
heres my code:


Code: Select all

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std; // using namespace STD


void characterSelection();
void enemies();
void humans();
void elves();
void dwarves();
void explore();
void save();
void open();
void mainMenu();

vector<string> mMenu;
string race; //global variable for chosing a race in characterSelection()
vector<string> Inventory;
string menuChoose;

int main () // main function
{



		mainMenu();
	
	if(menuChoose == "new game")
	{
		characterSelection();
	}
	if(menuChoose == "quit")
	{	
		return 0;
}
return 0;
}




void characterSelection()// character selection
{
	vector<string> raceSelection;
	raceSelection.push_back ("human");
	raceSelection.push_back ("elf");
	raceSelection.push_back ("dwarf");
	cout << "What would you like to be:\n";
	
	for (int i = 0; i < raceSelection.size(); ++i) //displaying the races you can choose from
		cout << raceSelection[i] << endl;
	cout << "Now type in the name of the race you want to choose and dont use capitals\n";
	cin >> race;
	
	cout << "Good choice\n";
	cout << "And now to begin\n\n";
	
	save();
	
	
	
	
	if ( race == "human")
	{
		Inventory.push_back ("worn dagger");
		Inventory.push_back ("tatered shirt");
		
	}
	if ( race == "dwarf" )
	{
		Inventory.push_back ("Worn battle axe");
		Inventory.push_back ("worn sheild");
		Inventory.push_back ("riped pants");
	}
	
	
	cout << "Displaying your inventory\n\n";
	
	for ( int i = 0; i < Inventory.size() ; ++i )
		
		cout << Inventory[i] << endl;
	
	
}
	

void save()// saving game
{
	
	
	ofstream fout("save.docx");
	
	fout << race << endl;
	
}


void mainMenu()
{
	
	mMenu.push_back ("New Game");
	mMenu.push_back ("Load game");
	mMenu.push_back ("quit");
	
	cout << "ERAGON ADVENTURE\n\n\n\n";
	cout << "Main Menu\n\n";
	
	for (int i = 0; i < mMenu.size(); ++i)
		cout << mMenu[i] << endl;
	
	cout << "What would you like to do (dont use capitals):\n";
	cin >> menuChoose;
}
every time i make the if statement with {} over characterSelection() and i type new game it just skips it and goes to logout and when i dont put the {} it just skips all the cins and goes to logout please help if you have anymore questions regarding this just ask :)

Edited by mod: Please use

Code: Select all

 tags.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help with random text adventure game c++

Post by dandymcgee »

"cin >> " will only take input up until the first space, so when you type "new game" only "new" will be stored in menuChoose. You'll need to either use commands without spaces, or getline instead of the cin stream in operator (>>).
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
adikid89
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 94
Joined: Tue Apr 27, 2010 6:59 am
Current Project: small tiny-mini projects
Favorite Gaming Platforms: PC I guess...
Programming Language of Choice: c++

Re: Help with random text adventure game c++

Post by adikid89 »

LOL :lol:

Code: Select all

using namespace std; // using namespace STD
useful comments are useful :lol:
My first game C++/SDL Yoshi Combat! = http://www.youtube.com/watch?v=HQ9mMBEWSZg
==============================================================
Image
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Help with random text adventure game c++

Post by eatcomics »

adikid89 wrote:LOL :lol:

Code: Select all

using namespace std; // using namespace STD
useful comments are useful :lol:
You should see my comments in java for my tech class :D I have fun
Image
richred20
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Nov 11, 2010 6:41 am

Re: Help with random text adventure game c++

Post by richred20 »

lol thanks and to the

using namespace std; // using namespace std

i was just bored thinking of what i should put in lol
richred20
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Nov 11, 2010 6:41 am

Re: Help with random text adventure game c++

Post by richred20 »

how do i save because my book does not explain it well and how do I make it so that that i will be able to store variables when you do the save function and load it with the load option without opening a file it just receives.

here is what i have so far:

Code: Select all

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;


void characterSelection();
void enemies();
void humans();
void elves();
void dwarves();
void explore();
void save();
void open();
void mainMenu();
	
ofstream fout("save.docx");		
vector<string> mMenu;
string race; //global variable for chosing a race in characterSelection()
vector<string> Inventory;
string menuChoose;
string helpRun;

int main () // main function
{



	
	mainMenu();
	
	if(menuChoose == "new")
	{
		characterSelection();
    if(race == "human")
    
            cout << "You wake up with the smell of burning,";
            cout << "And you worried that the town of Tierm was burning";
            cout << "You slowly get you pants on and you start feeling really hot";
            cout << "You look outside through the window, and see Tierm burning";
			cout << "You see ships in the bay and you know that Tierms is being raided";
			cout << "Your first instinct is to run to the barracks and hide with the soilders";
			cout << "but right as ou turn you see out of the corner of your eye a soilder killing a helpless man";
			cout << "fury invelops you as you put on your shirt and you take up your fathers dagger";
			
		cout << "what would you like to do now, try and help the villagers, or run nto the forest";
		cin >> helpRun;
		
			save();
			if( helpRun == "help")
			{
				cout << "You run outside weilding your dagger and as soon as you come across the first soilder he slits your throat and you die";
			return 0;
			}
	
		
    }
	if(menuChoose == "quit")
	{	
		return 0;
    }
    if(menuChoose == "load")
	{
		ofstream fout("save.docx", ios::in);
	
		cout << "your race is:\n" << race << endl;
		
	}
return 0;
}




void characterSelection()// character selection
{
	vector<string> raceSelection;
	raceSelection.push_back ("human");
	raceSelection.push_back ("elf");
	raceSelection.push_back ("dwarf");
	cout << "What would you like to be:\n";
	
	for (int i = 0; i < raceSelection.size(); ++i) //displaying the races you can choose from
		cout << raceSelection[i] << endl;
	cout << "Now type in the name of the race you want to choose and dont use capitals\n";
	cin >> race;
	
	cout << "Good choice\n";
	cout << "And now to begin\n\n";
	
	save();
	
	
	
	
	if ( race == "human")
	{
		Inventory.push_back ("worn dagger");
		Inventory.push_back ("tatered shirt");
		
	}
	if ( race == "dwarf" )
	{
		Inventory.push_back ("Worn battle axe");
		Inventory.push_back ("worn sheild");
		Inventory.push_back ("riped pants");
	}
	
	
	cout << "Displaying your inventory\n\n";
	
	for ( int i = 0; i < Inventory.size() ; ++i )
		
		cout << Inventory[i] << endl;
	
	
}
	

void save()// saving game
{
	


	
	fout << race << endl;
	
}


void mainMenu()
{
	
	mMenu.push_back ("New");
	mMenu.push_back ("Load");
	mMenu.push_back ("quit");
	
	cout << "ERAGON ADVENTURE\n\n\n";
	cout << "Main Menu\n\n";
	
	for (int i = 0; i < mMenu.size(); ++i)
		cout << mMenu[i] << endl;
	
	cout << "What would you like to do (dont use capitals):\n";
	cin >> menuChoose;
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help with random text adventure game c++

Post by dandymcgee »

Please either use pastebin or the

Code: Select all

 tags.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
richred20
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Thu Nov 11, 2010 6:41 am

Re: Help with random text adventure game c++

Post by richred20 »

please explain i am a noob with programing
Post Reply