Link to home
Start Free TrialLog in
Avatar of xhenkaii
xhenkaii

asked on

how to call another .cpp file?

hi... i've written 2 programs and saved them under 2 different names. and i'd like to create a 3rd, which acts like a menu to the 2. how do i go about writing the code so that when, for example, i type 1 and enter, and program 1 runs, and 2 for program 2...?

i hope i'm being clear enough... =) can n e one help? thx alot!!
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Getch
Getch

u want to call a .cpp file ? use a header file and include it in the main file.
read bjarne stroustrup or K & R book.
Avatar of xhenkaii

ASKER

thx alf, but i just started learning c++, so... i don't really know how 2 implement~

use a header file? what do i put in it? can i change my .cpp file to a .h file?
thx alf, but i just started learning c++, so... i don't really know how 2 implement~

use a header file? what do i put in it? can i change my .cpp file to a .h file?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For Alf's approach, you don't need to include anything from program 1 or 2 in your menu program. It works by running the already-compiled program 1 or 2. All you have to do is copy the program1.exe and program2.exe files from their directories into the same directory as your menu program.
thx alf, but i just started learning c++, so... i don't really know how 2 implement~

use a header file? what do i put in it? can i change my .cpp file to a .h file?
For the approach I showed you, which I interpreted was what you asked for is that you only need to have the programs somewhere in the path - or if you don't have them available in your path you can give full path names:

system("c:\\foo\\bar\\baz gazonk");

will run the program located by the name c:\foo\bar\baz and give it an argument of gazonk.

Note that if you have a directory name that contain spaces such as "program files" or some such you must give quotes:

system("C:\\\"Program Files\"\\bar\\baz gazonk");

Also notice that I use \\ for each \ and \" for a ". This is due to the way C compiler parses string literals.

You do not have to include the files for the other programs as your program simply start them up and run them in the same way you would start them at the command line yourself. You don't need any headerfile to type the name of the program and neither do system() need any header file of your programs.

It does, however, need an include file that declares the system() function and that might be system dependent. Under unix the function is found in stdlib.h or cstdlib. A good guess is therefore to first try

#include <cstdlib>

std::system("name of the program");

where "name of the program" is the line as I indicated previously.

If your program is named prog.exe and located in the directory c:\programs then the line is:

std::system("C:\\programs\\prog foo");

this will run program prog and give it an argument of 'foo'.

One drawback of this is that it might be difficult to interact with the program, you can intercept what the program output and you can also intercept what the program read from input but you can't really have a dialog going with the program.

An advantage is that the program you run is completely unaware that it is running under your control and is not started by a user typing the programname on the console.

If you want the other approach you have to change the source code of the two programs. They already have a main function and you have to rename that function to something else. Then you make a new main file and that mainfile will be something like this:

int main(....arguments here if you want...)
{
   if (you want to run programA)
      return Aprog_main(......);
   else if (you want to run programB)
      return Bprog_main(....);
   else
     return 0; // doesn't want to run any.
}

Here the main of progA is renamed to Aprog_main and the main of progB is renamed to Bprog_main.

These functions must be declared to the main program and the best way to do that is by making a header file for each program:

-------Aprog.h-----
int Aprog_main(....arguments here if you want...);
-------------------

and similarly for the Bprog_main function.

Then both the old main source for Aprog and the new main program include the Aprog.h and both the old main source for Bprog and the new main program include Bprog.h.

Also, you must be aware of possible name clashes, since you merge both programs into one you might have a conflict if both programs define a function with the same name and that name is not local to the program. In that case you can do one of the following:

1. Make one or both names local. This is not possible if one function in one file want to call the function but if the only call is from the same file that defines the function you can declare the function as static:

static return_type_here func(....);

This will declare function func as local to the file and it will not be known to the linker and in particular if there is another function in another file with the same name they will not cause any conflict. If that other function is external that function cannot be declared in the file defining the static function though as it will cause a conflict in that file.

2. Change the name of at least one of the functions that cause the conflict. You must then of course also change the name of the function in all places where it is called.

3. Place the function in a separate namespace. The caller can then either use a using statement like this:

using return_type_here foo::func(....);

if the function func is defined in namespace foo. Then a call to func will be to the function defined in that namespace. For example if both progA and progB has a function named:

int do_something(double x);

Then you can have progA's function inside namespace progA and progB's function inside progB. If a function want to call progA's function he can do:

int k = progA::do_something(3.14);

or you can place a:

using int progA::do_something(double x);

and then simply call do_something() as before:

int k = do_something(3.14);

Of course, you cannot do both:

using int progA::do_something(double x);
using int progB::do_something(double x);

will surely cause conflict.

Hope this explains.

Alf
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between Salte & DarthNemesis

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer