Using multiple .cpp files for programs

This is a discussion on Using multiple .cpp files for programs within the C++ Programming forums, part of the General Programming Boards category; Alright, this is what I'm trying to do: I want the main.cpp to have a menu for example saying for ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Using multiple .cpp files for programs

    Alright, this is what I'm trying to do:

    I want the main.cpp to have a menu for example saying for example:

    Menu:
    1. This is the first thing
    2. This is the second thing

    Then a simple input option and then I if they for example choose option one I want them to be redirected to.. let's say Option1.cpp and then when they're done there it needs to return you to main.cpp

    Please help me out doing this, cheers!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Do you know how to use functions? You don't redirect execution flow to another file, but call functions that are implemented in different cpp files and are available to main.cpp through declarations in header files (*.h, *.hpp).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Not quite sure what you mean..

    Like #include <Option1.h> or what?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    There's a FAQ on writing multi-file projects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Suppose you had all in one file:

    Code:
    //declarations - go into headers
    void option1(); 
    void option2();
    
    int main()
    {
        option1();
        option2();
    }
    
    //implementations may go into a separate cpp file
    void option1()
    {
        //implementation
    }
    
    void option2()
    {
       //implementation
    }
    A typical header might look like this:
    Code:
    //options.h
    
    //inclusion guards
    #ifndef OPTIONS_H
    #define OPTIONS_H
    
    void option1();
    void option2();
    
    #endif
    An implementation file which needs to know about these functions (to implement or use them), includes the header
    Code:
    #include "header.h"
    Each cpp file needs to be compiled and linked (easily achieved if they all belong to the project).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Alright, thanks alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header files and multiple files
    By Dedalus in forum C Programming
    Replies: 5
    Last Post: 06-16-2009, 09:21 AM
  2. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  3. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  4. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  5. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21