A question with different functions.

Joined
Dec 3, 2010
Messages
1
Reaction score
0
Okay, I have this sort of problem, please let me explain.

I have my main function obviously. From that main I wish to call another function that is another completely different .cpp file. Once I run this function I don't want the user to be able to access things outside of the function that was ran.

Code:
main.cpp
#include "Library.h"

int main()
{
    char choice[256];

    cout << "Welcome to Almost Life.\n";
    cout << "Please press enter to bring up the starting menu.\n\n";
    cin.ignore(1);

    cout << "1. Start new game.\n";
    cout << "2. Instructions.\n";
    cout << "3. Extras.\n";
    cout << "4. Exit Game.\n";

    while(choice[0] != '4')
    {
        cout << ">";
        cin.getline(choice,256);

        if(choice[0] == '1')
        {
            startgame();
        }

        if(choice[0] == '2')
        {
            cout << "Instructions will be displayed here.\n";
        }

        if(choice[0] == '3')
        {
            cout << "Extras will be displayed here.\n";
        }

        if(choice[0] == '4')
        {
            cout << "Thanks for playing, play again soon.\n\n";
        }

    }
    return 0;
}

startgame.cpp
#include "Library.h"

void startgame()
{
    cout << "This is the start of the game.\n";
}

Library.h
#ifndef LIBRARY_H_INCLUDED
#define LIBRARY_H_INCLUDED

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

// PROTOTYPES
void startgame();


#endif // LIBRARY_H_INCLUDED

Okay now what you see is that I call startgame(); but when I do this after I call that function I could type '2' and it would go to the if statement for '2' in main, and not in startgame.

Help, comments are greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top