?
Solved

how to call another .cpp file?

Posted on 2003-03-31
11
Medium Priority
?
302 Views
Last Modified: 2012-08-13
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!!
0
Comment
Question by:xhenkaii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
11 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 80 total points
ID: 8239248
If you have compiled both files with different program names then you can write a program to call each of them:

bool call_foo_or_bar()
{
   string cmd;

   if (we want to call foo program)
      cmd = "foo";
   else if (we want to call bar program)
      cmd = "bar";
   else
      return; // don't call any.
   system(cmd.c_str());
}

If you have files foo.exe and bar.exe available in your path this will call those programs.

Alf
0
 

Expert Comment

by:Getch
ID: 8239276
u want to call a .cpp file ? use a header file and include it in the main file.
0
 

Expert Comment

by:Getch
ID: 8239283
read bjarne stroustrup or K & R book.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:xhenkaii
ID: 8240491
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?
0
 

Author Comment

by:xhenkaii
ID: 8240494
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?
0
 
LVL 2

Assisted Solution

by:DarthNemesis
DarthNemesis earned 80 total points
ID: 8240767
You have basically two options here: Alf's approach, which is to have your new program run a DOS command to open either program, or you could change the other two programs into functions. Let's say you have two programs here:

#include <iostream>
using namespace std;
int main() {
        cout << "Hello" << endl;
        return 0;
}

and

#include <iostream>
using namespace std;
int main() {
        cout << "World" << endl;
        return 0;
}

You could copy the contents of both .cpp files into your new program's .cpp file, and rename the mains, like so:

#include <iostream>
#include <conio.h>
using namespace std;
int programA() {
        cout << "Hello" << endl;
        return 0;
}
int programB() {
        cout << "World" << endl;
        return 0;
}

int main() {
        cout << "Which program?" << endl << "(A/B):" << endl;
        char choice = getch();
        switch(choice) {
        case 'a':
        case 'A':
                programA(); break;
        case 'b':
        case 'B':
                programB(); break;
        default:
                cout << "Invalid choice." << endl;
        };
        return 0;
}
0
 
LVL 2

Expert Comment

by:DarthNemesis
ID: 8240780
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.
0
 

Author Comment

by:xhenkaii
ID: 8241931
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?
0
 
LVL 12

Expert Comment

by:Salte
ID: 8244257
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
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9566549
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question