Using Functions in a Text Adventure

Hey guys, so i have been trying to put together a lil text based adventure game been working on it for 2 hours now and i cant seem to use the cin or the getline(cin,variable) inside of my function. im using system("pause")
(everyones favorite topic i know but for the time being seeing as im not going to be releasing this code besides here i thought it safe) heres what i got:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream> 
#include <string>  
using namespace std; 
void East(){ 
	system("cls"); 
	string cd1, cd2, cd3;

	cout << "As you round the last bend down the windy trail you are greeted with the sound of seagulls and surf \n";
	cout << "Off in the distance to your right you can see a small fishing village, and off to your left you see a group of people down on the beach around a campfire.\n";
	cout << endl;
	cout << "So... Which way will you choose this time?\n" << endl << "Beach- Towards the strangers.\n  or\nTown- Towards the fishing village.\n\n";
	getline(cin,cd1);
	if (cd1=="beach"||"Beach")
		cout << "Ok, lets go see what these guys are up to!\n";
		system("pause");
}
int main(){ 
	string choice1, choice2, choice3, choice4, choice5;
	cout << "Ever since you can remember, "; //beginning of story goes here. 
	cout << endl;
	system("pause"); 
	cout << "\n"; 
	loop:
	cout << "The time has come for you to make a choice.\n \n Which way will you go?" << endl; 
	cout << " East- towards the coast \n West- towards the mountains \n South-towards the desert \n North-towards the Unknown Frontier\n"; 
	cout << endl;
	cin >> choice1;
if (choice1 == "east"||"East"){ 
	cout << "East it is then! Off to the coast you go...\n"; 
	system("pause"); 
	East();}
else if (choice1 == "west"||"West") 
	cout << "West huh? Pack a parka and let's go!\n"; 
else if (choice1 == "south"||"South") 
	cout << "Better bring some extra water, we're heading South for the winter!\n"; 
else if (choice1 == "north"||"North") 
	cout << "What a daring young man you are. I hope for your sake there is more of that courage...\n"; 
else { 
	cout << "Oops! Looks like you might have mispelled the direction you wanted to go :(\n"; 
	goto loop;} 
system("pause"); 
return 0;
}


lol i know some of you are going to freak when you see the goto but i didnt know else to return to that particular spot. i only have the east function going right now. thsi is the whole code and it compiles just fine.
Your if statements are wrong. The first, for example, says, "if the input is equal to "beach" or the char* string literal "Beach" is not NULL". I think you want if(cd1=="beach"||cd1=="Beach"), etc.
lol i know some of you are going to freak when you see the goto but i didnt know else to return to that particular spot


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
string choice1, choice2, choice3, choice4, choice5;
cout << "Ever since you can remember, "; //beginning of story goes here. 
cout << endl;
system("pause"); 
cout << "\n";

bool bBadChoice = true;
bool bEast = false;
bool bWest = false;
bool bNorth = false;
bool bSouth = false;

while(bBadChoice)
{
     cout << "The time has come for you to make a choice.\n \n Which way will you go?" << endl; 
     cout << " East- towards the coast \n West- towards the mountains \n South-towards the   desert \n North-towards the Unknown Frontier\n"; 
     cout << endl;
     cin >> choice1;

     bEast = (choice1 == "East") || (choice1 == "east");
     bWest = (choice1 == "West") || (choice1 == "west");
     bNorth = (choice1 == "North") || (choice1 == "north");
     bSouth = (choice1 == "South") || (choice1 == "south");

     bBadChoice =  !bEast && !bWest && !bNorth && !bSouth;

     if(bBadChoice)
     {
           cout << "Oops! Looks like you might have mispelled the direction you wanted to go :(\n";
     }
}

Last edited on
Hmm, thank you Zhuge :D but will that fix my problem of not being able to input my next choice? whenever i try to enter my choice between beach or town, the system pause is eating my input. how do i stop it from doing that?
Hey, if you are mixing cin >> with getline(cin, var). Remember to to a cin.ignore(10000,'\n')

cin >> reads the line but keeps the NULL or '\n' (enter) in the buffer, so the getline reads that and skips the user input.
you guys keep mentioning my if statements. i just dont understand why though. if i enter "east" or "East", my function east is called either way no problem. is there something im missing?

EDIT: please ignore, dumbest reply ever :) i realize know that shacktar was clarifying on my goto statement.
Last edited on
@Dukaim so what your saying is if im going to mix it up with cin >> and getline(blah) then i have to input cin.ignore(10000,'\n') somewhere in my code?
would i put this in the function or could i just plop into my main?
After the cin>> parts, as those leave a newline in the buffer.
Cool stuff guys :) i appreciate the help, i understand how "noobish" questions can get annoying. but as for doing the rest of the project, does anyone have any tips on the sort of syntax or style i should be using, or will the project be fine with cout, cin, ifs, and functions?
Just a little tip, look up the transform() function to convert a string to either uppercase or lowercase. Use it whenever your taking a string input that you have to compare to make a choice. Like let's say you wanna see if they typed "Beach" or not.

string choice;


cout << "Pick beach or blah blah blah" << endl;
cin >> choice
cin.ignore(100, '\n');
transform(choice.begin(), choice.end(), choice.begin(), toupper)

Then go on to do your if statement. This is important because a user can typo and do "bEach" or "BEAch" or any odd arrangement of uppercase letters, and "Beach" || "beach" will not cover all of those. So if you make whatever they wrote either all uppercase or all lowercase, and just make sure your if statement matches your transform function, everything will be great.
Topic archived. No new replies allowed.