Text adventure

Hello, I made a text application (command line tool) on xcode in C++ that lets you create a text adventure. In the int main you can "program" the text adventure. I made a void called game which is where the game starts once the user decides to test out his "text adventure". The problem is that when I run it void game does not understand the strings because they are in the int main. How would I explain to the void game what the strings are (for example: The games name, the authors name etc). Thank you.
This is a small preview:
String gamename;
getline(cin,gamename);
//More code...
void game (){
cout << "Welcome to" << gamename; - Use of undeclared identifier
}
when you change function you can't access local variables of another function.
So pass the values as argument to function game().Or use class or global variables
I do not understand, Can you please show a small piece of code using that?
So like this? //
// main.cpp
// Text creator 3
//
// Created by Olivier on 9/30/12.
// Copyright (c) 2012 Olivier. All rights reserved.
//

#include <iostream>
using namespace std;
void game( string name, string nameofgame )
{
cout << "Welcome to " << nameofgame;
cout << " by " << name;
}

int main(int argc, const char * argv[])

{
cout << "Name of game: ";
string nameofgame;
getline(cin,nameofgame);
cout << "Name of Author: ";
string name;
getline(cin,name);

game( name, nameofgame );
}
Or like this. Global variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 So like this? //
#include <iostream>

using namespace std;
string nameofgame;
string name;

void game() {
cout << "Welcome to " << nameofgame;
cout << " by " << name;
}

int main() {
cout << "Name of game: ";
getline(cin,nameofgame);
cout << "Name of Author: ";
getline(cin,name);
game();
}
Last edited on
Topic archived. No new replies allowed.