Getting System(); Command to Return Value

This is a discussion on Getting System(); Command to Return Value within the C Programming forums, part of the General Programming Boards category; I'm using int x; to return the value (0 or 1) from system(cmd); command. However, it always returns 0. Any ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    10

    Getting System(); Command to Return Value

    I'm using int x; to return the value (0 or 1) from system(cmd); command. However, it always returns 0. Any idea's? Here's the code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void)
    {
        char cmd[100];
        int ip_a, ip_b, ip_c, ip_d, i, x;
        
        printf("IP Address: ");
        scanf("%d.%d.%d.%d", &ip_a, &ip_b, &ip_c, &ip_d);
        
        if (ip_a < 0 || ip_a > 255) return 0;
        if (ip_b < 0 || ip_b > 255) return 0;
        if (ip_c < 0 || ip_c > 255) return 0;
        if (ip_d < 0 || ip_d > 255) return 0;
        
        for ( i = 0; i < 4; i++ ) {
            sprintf(cmd, "ping %d.%d.%d.%d -n 1 > nul", ip_a, ip_b, ip_c, ip_d);
            x = system(cmd);
            printf("X-Value = %d\n", x);
        }
        
        system("pause > nul");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    http://www.cplusplus.com/ref/cstdlib/system.html

    System() has two possible returns it looks like either 0 (or whatever your compiler evaluates to true) or -1 where -1 indicates an error

    so if it always evaluates to zero, that means its working just fine
    Last edited by ExxNuker; 01-12-2006 at 11:37 AM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    When you say "error," do you mean if the IP address doesn't exist, or if something is done incorrectly?

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    By error I mean something went wrong, whether it was that system couldn't open your command interpreter or some other reason....the way to tell what went wrong is to check errno (read through the link i posted it explains it pretty well)

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > However, it always returns 0. Any idea's?
    Whether system() returns anything useful is system dependent.
    DOS/Win32 is famously broken for always returning zero.

    Most unix systems successfully return the exit status of the program you ran.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    10
    Right now, I'm on a school computer, and it uses Microsoft Windows 2000. The one at my house uses Microsoft Windows XP.

    Anyways, is there another way of getting the return value (maybe a different method) that isn't broken on Windows-based systems?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    Maybe one of the other methods listed in the FAQ?
    Running programs within programs springs to mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  2. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 12:52 PM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-21-2003, 12:20 AM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21