BillyJean
BillyJean

Reputation: 1587

running external program and obtaining returned integer

I have the following MWE, which returns an integer:

#include <iostream>
using namespace std;

int main()
{
    int a = 2;
    return a;
}

Now I want to call this program via the command line (cmd) in Windows. Here is a program for how I do that:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{
    int a = system("c:\test_batch.exe");
    cout << a << endl;
    return 0;
}

However this does not return the value 2, but 0. I don't understand this as I thought system() returned the exit-code of the program, in this case 2.

Upvotes: 0

Views: 115

Answers (1)

user1773602
user1773602

Reputation:

system returns a value returned by a command interpeter, not by actual command.

you need to do something like

int a = system("c:\test_batch.exe && exit %ERRORLEVEL%");

Upvotes: 1

Related Questions