Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,945 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,412 people online right now. Registration is fast and FREE... Join Now!




System() Problem/Question

 
Reply to this topicStart new topic

System() Problem/Question, System("Ping") with user enters IP address

lockdown
6 Jan, 2008 - 06:11 PM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
So I am creating a application that allows the user to enter in a IP address and the program will ping it.

I will be using the System("ping") command but need to allow it to take a IP address the user enters. I was thinking of doing something like this but dont know how to formate it

CODE

    cout << "Enter IP address in xxx.xxx.xxx.xxx formate" <<endl;
    getline(cin,userString);
    system(userString.c_str(ping)); // Possible system(userString.c_str(), ping)


But I do not believe that work work. How would it be formated for that to work properly?
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: System() Problem/Question
6 Jan, 2008 - 06:27 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,590



Thanked: 12 times
Dream Kudos: 325
My Contributions
In C you could do it like this (minus the user input, but you would just read that into the "ip_addy" string).

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>

int main()
{
  char* ping = "ping ";
  char* ip_addy = "127.0.0.1";
  char* s = malloc(strlen(ping) + strlen(ip_addy));
  
  strcat(s, ping);
  strcat(s, ip_addy);
  
  system(s);

  return 0;
}

There's probably a cleaner way in C++ though...
User is online!Profile CardPM
+Quote Post

lockdown
RE: System() Problem/Question
6 Jan, 2008 - 06:39 PM
Post #3

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Thanks for the response. I have a limited knowledge of working with C but would like my program all C++ based even know your code would work. Dose anyone have the way to perform this in C++?
User is offlineProfile CardPM
+Quote Post

Xing
RE: System() Problem/Question
6 Jan, 2008 - 06:40 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 4 times
Dream Kudos: 1575
My Contributions
Try this
system(("ping " + userString).c_str());
User is offlineProfile CardPM
+Quote Post

lockdown
RE: System() Problem/Question
6 Jan, 2008 - 06:47 PM
Post #5

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(Xing @ 6 Jan, 2008 - 07:40 PM) *

Try this
system(("ping " + userString).c_str());


That worked perfectly. Thanks for all the help guys.
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: System() Problem/Question
6 Jan, 2008 - 06:48 PM
Post #6

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

How 'bout this?



CODE

#include <string>
using namespace std;

int main ()
{
     string userString;
     cout << "Enter IP Address: ";
     cin >> userString;
     userString = "ping " + userString;

     // may have to include a library for c_str, can't remember.
     system (userString.c_str ());

     return 0;
}




I believe the above, if the user enters 127.0.0.1, is equivalent to:

CODE


system ("ping 127.0.0.1");



Is this what you are trying to do?





Looks like someone beat me to it!
User is offlineProfile CardPM
+Quote Post

lockdown
RE: System() Problem/Question
6 Jan, 2008 - 06:59 PM
Post #7

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
QUOTE(VernonDozier @ 6 Jan, 2008 - 07:48 PM) *

How 'bout this?



CODE

#include <string>
using namespace std;

int main ()
{
     string userString;
     cout << "Enter IP Address: ";
     cin >> userString;
     userString = "ping " + userString;

     // may have to include a library for c_str, can't remember.
     system (userString.c_str ());

     return 0;
}




I believe the above, if the user enters 127.0.0.1, is equivalent to:

CODE


system ("ping 127.0.0.1");



Is this what you are trying to do?





Looks like someone beat me to it!


Technically you could uses that but when working with strings it better to uses the getline(cin, ) to get the data. I would uses cin but I am developing the user entering in more then 1 IP and need to have a string so I can manipulate the elements of the string (string is basically a character array)
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 05:00PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month