Passing string from function to another

Hi I'm trying to create a text based game where you have multiple choises and you choose 1 option. I use switch to see what option I chose and after dealing with the option I change the options string value to something else and return to the function where you choose your option. The string value changes into the new value but after going back to the other function, the value goes back to the original.

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
44
45
46
47
48
49
50
51
#include "stdafx.h"
#include "iostream"
#include "string"
#include "stdio.h"

using namespace std;


string option1 ("1. Wait, where am I?");
string option2 ("2. Who are you?");
string option3 ("3. What are you?");

int answer;

void choise(){
	cout <<"\nEnter your answer: ";
	cin >> answer;
}
void stage1(){
	
	cout <<endl << option1 <<endl << option2 <<endl << option3;
	choise();
	switch (answer){
	case 1:
		{cout <<"\n??? - You are in Human Storageroom #34. The date is the 2nd of January 2142.\n";
		string option1 ("Human Storageroom? What is that?");
		stage1();
		break;}
	case 2:
		cout <<"\n??? - I am Guro. That is short for Guriitaa Robotto, Greeter Robot in English.";
		break;
	case 3:
		cout <<"\n??? - I am a robot specially made for greeting people like you.";
		break;
	default:
		stage1();
		}
	
}
void main(){

	cout <<"??? - Welcome to the future!\n";
	stage1();




	getchar();
	getchar();

}


As you might see, I'm trying to change the string option1 value. I tried to google my problem but my terms obviously don't match the correct c++ terms. Don't worry about other cases etc. as I'm just trying to get it work on case 1 first.
I could ofcourse just make huge else if trees but that does not appeal to me.
Last edited on
well the problem is you're not actually changing the value of your global variable called option1.
Inside case 1 when you do :

string option1 ("Human Storageroom? What is that?");

what that does is it creates a new local variable called option1, even if you have a global variable with the same name, they're 2 different variables, and changes in one doesn't affect the other. So basically, you aren't really assigning it a new value, if you want to assign it a new value instead you should do something like:

option1 = "Human Storageroom? What is that?";
Last edited on
Thank you :) I'm a bit embarassed it was THAT simple. Oh well at least it works now as intended. I actually had not used string stringname ("blaa"); before and was really confused why it didn't transfer to other functions. Good ol' stringname = "blaa"; works as I remembered :') (I had a 6 month brake from learning).
Last edited on
Topic archived. No new replies allowed.