-5

Im a complete noob with C++ and i was wandering if this is possible, I would like to switch from int main() to int game() im not sure if its entirely possible.

    #include <iostream>
#include <cstdlib>
using namespace std;
string name;


int main()
{
    //So this is where we get the users name.
    string namechoice;

    cout << "Hello adventurer." << endl;
    cout << "What is your name?\n";
    cout << "\n";
    cout << "\n";
    cout << "\n";
    cout << "\n";
    cout << "                               Name: ";
    cin >> name;
    system("CLS");
    cout << "So your name is " << name << ", Correct? (Y / N)\n";
    cin >> namechoice;
    namechoice[0] = toupper(namechoice[0]);
    system("CLS");
    if (namechoice == "Y"){

    }
    else if (namechoice == "N"){
        while(namechoice == "N"){
            cout << "Please enter your name: ";
            cin >> name;
            system("CLS");
            cout << "You're name is " << name << ", Correct? (Y / N)\n";
            cin >> namechoice;
            namechoice[0] = toupper(namechoice[0]);
            system("CLS");



        }
    }
}
int game()
{
        cout << "Test";

        return 0;
}

So what im asking is how do i go to int game() if either of the conditions i have in int main are eventually fulfilled.

Thanks in advance!

  • 2
    Just put `game();` where you want. It will call the respective function, transferring control to it. Though if you want to keep the `game` function after your `main`, you'll need a forward declaration like `int game();` before `main`, so that `main` knows how it looks like, and how to invoke it. – jweyrich Jul 20 '14 at 01:18
  • Posted as an actual answer that includes a little example. By the way, would you like to read a good introductory book? We have plenty listed on [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), and on [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I hope it helps :-) – jweyrich Jul 20 '14 at 02:01

2 Answers2

0

Just put game(); where you want. It will call the respective function, transferring control to it. Though if you want to keep the game function after your main, you'll need a forward declaration like int game(); before main, so that main knows how it looks like, and how to invoke it.

For example:

int game(); // forward declaration

int main() {
    if (condition) {
        // calling a function transfers control to it.
        int result = game();
        // After its return, the flow continues from where we _stopped_.
        // You can print game's result, if you wish.
        cout << result << "\n";
    } else {
        // do something else
    }
    return 0;
}

int game() {
    cout << "Test\n";
    // _return_ will "terminate" this function's flow and return control back
    // to the caller function.
    return 0; // This value will be returned to the caller function.
}
jweyrich
  • 31,198
  • 5
  • 66
  • 97
0

You need to add #include <sstream> for inputting strings! You can add the game function anywhere, it will be called on the line it has been scanned on. Also so good practice for is to add the main function at the bottom, as well as using the void keyword. Here is my example:

void game(){
       cout<<"Test";
    }

int main(){

       game();

return 0;
    }
lifrah
  • 27
  • 4