LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-02-2007, 05:51 PM   #1
c4nk
Member
 
Registered: Oct 2006
Distribution: CentOS 4 :) and Win Xp ;(
Posts: 32

Rep: Reputation: 15
Pinging a user defined veraible in C++


Hi,
I am creating an console ap that will ask user to input an ip address and then its gonna ping it,
and I am stuck at the point of the ping command because i dont know how can i add the user veriable to the ping command itself.
Here is the code.

Code:
#include <stdlib.h>
using namespace std;
int main()
{
   system("cls");
   string ipaddress = "a";
   cout<<"Input an IP address or a name of a host:\n";
   cin >> ipaddress;
   system("ping ", ipaddress);                         //I also tried system("ping " & ipaddress);
   cin.get();
}
I ever tried something like this:

Code:
#include <stdlib.h>
using namespace std;
int main()
{
   system("cls");
   string ipaddress = "a";
   cout<<"Input an IP address or a name of a host:\n";
   cin >> ipaddress;
   ipaddress = "ping ", ipadress;
   system(ipaddress);
   cin.get();
}
If i could get some help on this it be very nice couse this was bugging me for quite a while and I have no clue what the solution to this might be.

Last edited by c4nk; 04-02-2007 at 05:54 PM.
 
Old 04-02-2007, 08:56 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,538

Rep: Reputation: 58
Code:
   ipaddress = "ping ", ipadress;
what were you trying to do here?

Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
   system("cls");
   string ipaddress;
   cout<<"Input an IP address or a name of a host:\n";
   cin >> ipaddress;
   string cmd = "ping " + ipaddress;
   system(cmd.c_str());
   cin.get();
   
   return 0;
}
this is C++, so you dont use .h files. for the "system" function it requires a C-string (char*), that is why you need the <string>.c_str().
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
user defined shell commands ashley_31 Linux - Software 11 05-15-2006 03:27 AM
user defined service Tonatiuh Linux - General 2 03-22-2006 01:44 PM
User defined protocols with Guarddog Shafted Linux - Security 4 09-01-2005 12:56 PM
user defined variables linuxdev Linux - Newbie 4 02-09-2004 12:43 PM
C++: How to use a namespace in a user-defined library? Gethyn Programming 9 08-15-2003 05:29 AM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration