Start a program from within a program.

Does anybody know where I can read about how to start a program from within a program. Then have the secondary program return a value to the main program when it's complete?

a simple example of how to do that would be helpful if there isn't a tutorial some where. I can usually poke around with something simple until I get it to work the way I want.
Last edited on
There are ways to actually do this, but these ways are unnecessarily complected.

I think what you are looking for is how to make functions:

1
2
3
4
5
6
7
8
int myFunction ( ) {
     return 5;
}

int main {
     cout << myFunction();
     return 0;
}


-Blueberry
My issue is I want to have one program parsing no matter user input.

The other program would keep track of time. When the timer program is complete it would send back a value to let the parse program know to play the music again.


I want it to be this way so I can loop a midi music file again if the parser hasn't met a "game zoning message". I need it to keep checking the log though while it's keeping track of time.

I think doing it this way would be helpful for other things also. If I don't add anything else to the timer program, I can probably use it for other programs that need to keep track of time while performing other actions.

If I tried to put a timer into the parse program, the parsing would stop while the timer was counting.
Last edited on
Instead of using a timer, you could try checking to see if a certain amount of time has elapsed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <ctime>
using namespace std;

int main () {
     clock_t ticksSinceStart = clock();
     
     clock_t targetNumberOfClicks = 15 * CLOCKS_PER_SEC;

     while (true){
     /*CODE THAT DOES OTHER STUFF*/

     if (clock() - ticksSinceStart > targetNumberOfTicks){
            /*DO SOMETHING*/
     }

     }
}


Again, although running 2 programs, and having the 2 programs communicate with each other is possible, generally it is both difficult and unnecessary.

Another possible way to solve this problem would be use threads, but again: difficult and unnecessary.

To make it so that you can use these timer functions in future programs, you can perhaps make a library of functions that makes it convenient use the time in a program. This library can then simply be included in future programs that use it.

-Blueberry

EDIT: Made corrections to code. I am really too sleepy.
Last edited on
Thanks for the response. A clock function might be what I can use for this particular problem.

I tried to set up a simple program to print out a message every 15 seconds but I keep getting a compile error.

I was basing the code below off this reference program here.

http://www.cplusplus.com/reference/ctime/clock/


Not sure what I'm doing wrong. The compiler says "expected primary expression before t".

I'm assuming because of this I would actually have to make a time.h file on my own to make the clock function work? If that's the case how would I go about doing that?
Last edited on
I would start off with removing the #include <ctime> . It's essentially the same thing as #include <time.h> , except you have to use using namespace std;.

The part that is causing the error, however, is the fact that you forgot to put the ( ) in int main ( )


Also, I don't quite understand what you are trying to do with the clock.

The clock is not a timer function, or anything. Think of it simply the number of ticks since the program started running. You find out how much time elapsed by reading the time at the beginning, and comparing with the time you read at the end of something.

This means you would need to check the difference between the time that you last printed the statement, and the current time.

Also, never compare time using a == statement. Think about it, what is the likelihood of the time being checked at exactly 15.0 seconds?

-Blueberry

EDIT: Oh shoot..I forgot it too in my earlier post. I am editing it now.
Last edited on
edit.. nvm I figured this out.

When I get tired and frustrated I can't think straight.

You helped me understand what I was missing so thanks again :)

This would be a simple program to do what I was looking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <ctime>
#include <iostream>

int main(){
     clock_t t,x;
     t = clock();
     while (true){
     /*CODE THAT DOES OTHER STUFF*/
     x = clock();
     if (((((float)x)-((float)t))/CLOCKS_PER_SEC) > 5.0){
            std::cout << "5 seconds elapsed\n";
            t = clock();//reset for new cycle
     }

     }
}
Last edited on
Topic archived. No new replies allowed.