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:
#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
"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!
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.
#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;
}