There's nothing wrong with using system() for its intended purpose -- though you really
should use CreateProcess() [on Windows] or fork() and exec*() [on *nixen].
Every program has a
current working directory attached to it -- where in the directory tree it thinks it is. This CWD gets passed to any process invoked via system(). If you want a child process to be given a different CWD, your options are, in order of increasing madness:
- change the CWD before running the child process (and changing it back when you are done)
- give a command string that changes the directory AND starts the child process
- have the child process set its own CWD
To change the CWD from your program you can use Boost Filesystem, or you can use the old chdir() POSIX function (#include <unistd.h> on *nixen or <dirent.h> on Windows).
To
compile a C++ program from your program, you will need to invoke the user's compiler, just as you would from the command-line. This is actually not a very easy task, particularly on Windows.
It kind of looks to me like you want to write a shell interpreter. That's an interesting project. There is a lot to find on the internet (
https://www.google.com/search?q=write+your+own+shell). A good one to start is this one:
http://stephen-brennan.com/2015/01/16/write-a-shell-in-c/
Brennan's is for C, so you can easily update it to use C++ techniques. For example, instead of using strtok(), use a split function:
http://www.cplusplus.com/faq/sequences/strings/split/
Also, his tutorial is for Linux, so you won't want to use fork() and exec*() on Windows. Use CreateProcess(). It's a scary-looking function, but read through it and use the defaults for everything you can:
http://www.google.com/search?btnI=1&q=msdn+CreateProcess
Good luck!