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.
#include "stdafx.h"
#include "iostream"
#include "string"
#include "stdio.h"
usingnamespace 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.
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:
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).