| youngcoder13 (21) | |
|
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 } | |
|
|
|
| Akshit (190) | |
|
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 | |
|
|
|
| youngcoder13 (21) | |
| I do not understand, Can you please show a small piece of code using that? | |
|
|
|
| youngcoder13 (21) | |
|
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 ); } | |
|
|
|
| Shinigami (288) | |||
Or like this. Global variables.
| |||
|
Last edited on
|
|||