Page 1 of 1

How to make a Program to Ping in C++

Posted: Thu Dec 02, 2010 5:47 pm
by Nipuna
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;
}

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

Posted: Thu Dec 02, 2010 9:43 pm
by Neo

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;
}

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

Posted: Fri Dec 03, 2010 10:22 am
by Nipuna
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.

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

Posted: Fri Dec 03, 2010 11:21 am
by Neo
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.

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

Posted: Fri Dec 03, 2010 2:21 pm
by Nipuna
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. :)

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

Posted: Fri Dec 03, 2010 2:34 pm
by Neo
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.

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

Posted: Fri Dec 03, 2010 5:51 pm
by Nipuna
Sorry Neo I made a Mistake not Strings. I wanted to tell it was Pointers. :) Sorry For that