I've written a few simple C++ programs in Dev-cpp... Now i want to run the second program inside the first in case it satisfies a condition. It then geneerates a text file from which new values are got. Is it possible to do that ? I'm just a beginner and don't want to do any complicated things right now. And i can't just copy paste the code onto the first one because a lot of values and variables will conflict with each other.

Really need help on this
Thanks for your help..

Recommended Answers

All 2 Replies

you can execute other programs with the system() function or with several os-specific api functions. For MS-Windows it might be either ShellExecute() or CreateProcess().

Without seeing your code it is a tricky question to answer.

What you probably want to do is put both programs into
classes then you can
create an instance of the class and decide whether to run it on the if

say a simplified version of your code looked like this

#include <iostream>

int main
{
//program 1 start
int a(1), b(3);
double c= -1.3;
std::cout << "a+ b = " << a + b << " c => " << c << std::endl;
return 0;
}

now a basic class would then become
program1.h

class program1
{
 program1();
 void init(int na, int nb, double nc);
 int run();
int a, b;
double c;
};

program1.cpp

program1::program1()
: a(0), b(0), c(0.0)
{
}
//set all your variables
void program1::init(int na, int nb, double nc)
{
 a = na;
 b = nb;
 c = nc;
}

int program1::run()
{
//program 1 start
std::cout << "a+ b = " << a + b << " c => " << c << std::endl;
return 0;
}

and then you can call in main

#include "program1.h"

int main()
{
 bool use_p1(true);
 program1 pr1;

 if(use_p1 == true)
 {
    pr1.init(1, 3, -1.3);
    int ret = pr1.run();
  }
  else
  {
     /*
       do the same for other program
    */
  }

return 0;
}

classes are usefui and should have a clearer design than this abstraction.

It is possible to call .exe from inside code but it gets messy and Ithink the classes is what you want.

I've written a few simple C++ programs in Dev-cpp... Now i want to run the second program inside the first in case it satisfies a condition. It then geneerates a text file from which new values are got. Is it possible to do that ? I'm just a beginner and don't want to do any complicated things right now. And i can't just copy paste the code onto the first one because a lot of values and variables will conflict with each other.

Really need help on this
Thanks for your help..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.