Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4543782
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:34:46+00:00 2026-05-21T15:34:46+00:00

I have a program, it starts in a winmain which calls run_game in its

  • 0

I have a program, it starts in a winmain which calls run_game in its loop.

Because I treat run_game like a main function I am having to define many global variables.

The win main is in one file and everything is in another .cpp file. It started as a tutorial from a book and I have kept adding to it for a while now. Because my code is getting so big I want to break it into several .cpp files but I have a few question about doing so.

  1. I see a lot of people having one giant source file. I am self taught so not sure if this has a benefit or is just style.

  2. People say globals should be avoided so how can I make variables I declare in functions available in other files.

  3. Is my program in a function approach a bad way of doing it as my plan was to break my run_game function into several smaller functions, each function would than be a state of the game.

I know people may complain this is vague so if you vote to close please comment on how I could make the question clearer.

Edit: I do know how to use classes and have 4 classes and 3 structs in the header file for this program, but an instance of a class acts the same as a variable so does not solve the problem of scope.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:34:46+00:00Added an answer on May 21, 2026 at 3:34 pm

    I see a lot of people having one giant
    source file. I am self taught so not
    sure if this has a benefit or is just
    style.

    Its usually better to have smaller well defined files instead of one giant source file. Each file would either have a single C++ class definition or a set of interrelated functions. A rough guideline is no more than a few hundred lines of C++ code, but its subjective and difficult to quantify. You may often need much less or a lot more.

    People say globals should be avoided
    so how can I make variables I declare
    in functions available in other files.

    You need to pass those variables as parameters to those functions. For example if run_game calls foo, and foo needs to know the current score, pass the score in as an argument, like so:

    void run_game()
    {
         int score = 0;
    
         ...
         foo(score);
    }
    
    
    void foo(int score)
    { 
        // do some stuff
    }
    

    This is known as “pass by value”, foo is getting its own copy of score. So if foo updates score, the caller’s copy won’t get updated. You can let foo update score by using pass by reference as so:

    void foo(int& score) // notice the ampersand
    {
        // add 100 pts
        score += 100;
    }
    

    Now you may ask, well I’ve got 50 variables, how could I possibly pass all 50 into foo? This is where the hard work of software engineering comes into play. You have a couple of options. In any option, you want to think about how pieces of variables group together. You might find you’re always passing the same 5 variables into the function foo. Maybe its some representation of the players accomplishments so far in the game. The simplest way to gorup these together is with a struct:

    struct PlayerAccomplishments
    {
        int playersScore;
        int numGoblinsSlayed;
        int numTrollsSlaved;
    };
    

    Now you can declare variables of this type, set the specific fields, and pass them around:

    void run_game()
    {
         PlayerAccomplishments accomplishments;
    
         ...
         foo(accomplishments);
    }
    
    
    void foo(PlayerAccomplishments& playerAccomps)
    { 
        // do some stuff
        playerAccomps.numTrollsSlayed++;
        playerAccomps.playerScore += 100;
    }
    

    The next step in abstraction would be wrapping a set of variables and common pieces of functionality into a class. This lets us easily share common functionality and enforce a safe, well way for manipulating these objects–as defined by that object’s member functions. So for example if we had a player class.

    class Player
    {
    private:
        int score;
        int numTrollsSlayed;
    public:
        // safely update the player's score after slaying a troll
        void SlayTroll(Troll& aTroll);
    };
    
    // in the cpp
    Player::SlayTroll(Troll& aTroll)
    {
        score += 100;
        numTrollsSlayed += 1;
    }
    

    Then the code above becomes:

    void run_game()
    {
         Player aPlayer;
    
         ...
         foo(aPlayer);
    }
    
    
    void foo(Player& aPlayer)
    { 
        Troll someTroll;
        aPlayer.SlayTroll(someTroll); // updates aPlayer's score
    }
    

    I hope you can see that there’s a lot of ways to think about these problems, and its not easy to figure out how to seperate things out. This answer barely scratches the surface. I recommend a good course or book in software engineering. This stuff is hard. Really smart people with decades of experience struggle with the best way to organize and write software. But its great to think about this stuff. Its good to work hard to find ways to improve your code so you’ll be able to make sense of what you’re trying to do today, tomorrow, and six months from now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that when it starts, opens a winform (it is the
I have a simple script which is used to start another program. This other
I have a program which needs to behave slightly differently on Tiger than on
I have a simple Tray icon program that opens a site using System.Diagnostics.Process.Start(URL) And
I have a ClickOnce environment like this: \\Fileserver\ClickOnceApps\App1.application C:\Documents and Settings\user\Start Menu\Programs\publisher\app1.appref-ms My understanding
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a program 'foo' running different threads, fooT1, fooT2, .. fooTn. Now if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.