How to make a Program to Ping in C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

How to make a Program to Ping in C++

Post by Nipuna » Thu Dec 02, 2010 5:47 pm

I got Ping Idea From Saman. And I learned about it from Neo too. Both teached me well. :)

Today I thought to make a Simple Program for Fun. I haven't started learning C++ yet but I learned a little and i will learn the rest in later Day.

Then I tried to make a Program Like this

When user Input an URL program Pings it and then Show ping Results.

I made a Program but it doesn't work well it always says "Enter URL". I entered an URL but it still says "Enter URL"

And I think MY code is Wrong. Can you Give Me the Correct code?

Here is the Code

Code: Select all

#include<iostream>
#include<string>
using namespace std;
int main ()
{
    string url;
    float ping;
    cout<<"Enter URL";
    cin>>url;
    ping=system("ping"),url;
    cout <<ping;
    cin.get();
    return 0;
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to make a Program to Ping in C++

Post by Neo » Thu Dec 02, 2010 9:43 pm

Code: Select all

#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string url;
	string cmd;

	cout << "Enter URL" << endl;
	cin>>url;

	cmd = "ping " + url;

	system(cmd.c_str());

	cin.get();

	return 0;
}
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: How to make a Program to Ping in C++

Post by Nipuna » Fri Dec 03, 2010 10:22 am

I know this is a function

Code: Select all

cmd = "ping " + url;
Isn't it?

And What is this

Code: Select all

system(cmd.c_str());
and what is it doing?

I tried adding this

Code: Select all

cmd="pause";
without

Code: Select all

system(cmd.c_str());
. but it only prints pause on the screen i think this one

Code: Select all

system(cmd.c_str());
does the ping+url work.

Thanks for your time for helping me.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to make a Program to Ping in C++

Post by Neo » Fri Dec 03, 2010 11:21 am

I know this is a function

Code: Select all

cmd = "ping " + url;
Isn't it?
In C++, it is possible to concatenate strings (add strings) in this way.
So what happens here is that we construct a string like "ping 192.168.0.1" and store it to the string var cmd.
And What is this

Code: Select all

system(cmd.c_str());
and what is it doing?
system() is used to execute external EXEs, DOS commands, etc...
If you add system("dir"); this will list down the directories in the current directory (same as you type dir in the command prompt).
Since cmd is a C++ string, it is required to use cmd.c_str() to get the pointer of the string.
So what we do with system(cmd.c_str()); is that we execute "ping ...." command on the command prompt. That's all.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: How to make a Program to Ping in C++

Post by Nipuna » Fri Dec 03, 2010 2:21 pm

Thanks Buddy
system() is used to execute external EXEs, DOS commands, etc...
If you add system("dir"); this will list down the directories in the current directory (same as you type dir in the command prompt).
Since cmd is a C++ string, it is required to use cmd.c_str() to get the pointer of the string.
So what we do with system(cmd.c_str()); is that we execute "ping ...." command on the command prompt. That's all.
Pointer Means the Memory Address of the cmd string? Isn't It?

I didn't Learn till Strings. :)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to make a Program to Ping in C++

Post by Neo » Fri Dec 03, 2010 2:34 pm

Pointer Means the Memory Address of the cmd string? Isn't It?
string is a class in C++ specially created to hold a string and provide more flexibility on string operations. The memory pointer that contains text is returned by that command.

Since you have not yet stepped in to Object Oriented Programming concepts (OOP) you don't need to learn concepts like class, object, etc... right now. Just use it and later when you come to classes you'll learn them.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: How to make a Program to Ping in C++

Post by Nipuna » Fri Dec 03, 2010 5:51 pm

Sorry Neo I made a Mistake not Strings. I wanted to tell it was Pointers. :) Sorry For that
Post Reply

Return to “C/C++ Programming”