Text Based Game Attempt

Hello All. I am a novice programmer, and as such, might be getting in over my head with this, but I am attempting to write a text based game for my younger brother, who happens to be a huge Star Wars geek. The idea is to build something in the vein of a Choose Your Own Adventure book. I am having trouble with two things so far.

1) I am trying to declare a string, allow the user to input his name "Joe Smith," then use that string throughout the story. for some reason, I keep getting errors when I use:

using namespace std;

string playername;
cin >> playername;

What am I doing wrong here?


2.) I have it set up so that when he makes certain bad decisions, his character dies, and the game is over. What method should I use to reset the program to the beginning? Is there a way of resetting upon a certain event?

Thanks for the help.
What am I doing wrong here?


The thing about errors is that they actually will tell you exactly what the problem is if you know how to read them. Post the error message on here so that we can see what you're doing wrong.

Is there a way of resetting upon a certain event?


You should separate the game logic from the game data. Ideally, each "page" would just be something like a text file (or a few lines in a text file) and your program should just read the appropriate text file at the appropriate time. You should not hardcode the text into your actual program logic -- as that leads to a lot of copy/pasting, redundant code, and often hard to follow logic.

I made a post explaining how to do this for simple text adventure games a while back. See this thread:

http://www.cplusplus.com/forum/beginner/121532/#msg661754

Note that what you're trying to do is even simpler... so it shouldn't be too hard, but don't feel bad if you don't understand it. I'm happy to answer Qs about it.
did you include <string>

#include <iostream>
#include <string>
Last edited on
ah for the record, the issue was that I was declaring the string outside of the function. smh
Topic archived. No new replies allowed.