0

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.

1
  • 1
    Search for IPC - inter process communication. Commented Oct 31, 2012 at 12:20

1 Answer 1

1

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%");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.