Using variables in the system function

This is a discussion on Using variables in the system function within the Windows Programming forums, part of the Platform Specific Boards category; How can I use a variable in a system function? i.e. I want to make a simple little program that ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    66

    Using variables in the system function

    How can I use a variable in a system function?
    i.e. I want to make a simple little program that you can put an IP address into the program and it will ping it for you. I would do something like this:
    Code:
    int main()
    {
    double IP = 0.0;
    cout << "Please Enter the IP Address you Wish to Ping: ";
    cin >> IP;
    
    system("ping");
    }
    Where would I put the variable IP?

    Also, would the IP be a double, seeing as there are more than one decimal place?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    It wouldn't work using a variable of type double since an IP contains more than one dot, which doesn't fit the format of a double. You want to use a string instead. As for the command, you should use a string containing "ping your_ip_address".

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    What exactly would I do with that string?
    Simply printing it out in the console won't do anything... or would it..?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You want to create buffer

    command[MAX_COMMAND_LEN] of the enough lenght to store the whole command
    Then you will use sprintf to build the string

    sprintf(command,"ping &#37;s", ip_address);

    where ip_address is a string with desired IP

    and call

    system(command)

    Instead of sprintf you can use snprintf (_snprintf on MSVC) to prevent buffer overflow or allocated command buffer dynamically based on the resulting command len

    Or you can use some socket library (like winsock2) and build a ping packet by yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Recently asked - Using variables in system()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    66
    OK, thanks. I did use the search but just for this section. Also, I am using this in C++ and I see that that thread is in the C Programming section. There must be a difference.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Only in the way(s) you can construct the string to send to system().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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