QuestionPinging an IP ? [Solved]

Posts 1–11 of 11 · Page 1 of 1
Pinging an IP ? [Solved]
Back with another question.

I want to ping 8.8.8.8 (google DNS) to assure I am online, but I am not positive of the commands / syntax of the "if" statement. I'm aware this isn't correct, but what I mean is:

Code:
int main()
{
	system("ping 8.8.8.8");
		if(pingSuccess == 1) {do this}
}
pingSuccess is not a variable in my code, I'm not sure if I need one or there is a command to detect true/false from the closest command? Then I can code the rest. Yeah, it's simple. Yeah, I could do it in a batch file. But there's no harm in learning new syntax / commands.

Don't need full source codes, since I didn't tell you what this is for. It's a surprise.

EDIT: Here's my new source:
Code:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

static string host;
static string ping_again;

void _doPing()
{
     system("cls");
     cout << "Enter the IP you wish to ping: ";
     cin >> host;
     system (("ping " + host).c_str());
     cout << endl;
     cout << "(Y)es  (N)o\n\n";
     cout << "Ping another host: ";
     cin >> ping_again;
     if (ping_again == "Y" || ping_again == "y")
     {
     system("cls");
     ping();
     }
     else if (ping_again == "N" || ping_again == "n")
     {
     system("exit");
     }
}

int main()
{
     ping();

     return(0);
}
EDIT2: I still need to have an if statement that will execute commands if successfully pinged the user-inputted IP.
[highlight=c]#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
static string host;
static string ping_again;
void Ping()
{
cout << "Enter IP you wish to ping: ";
cin >> host;
int result = system (("ping " + host+ " > nul").c_str() );
if (result==0)
{
cout << "Connected successfully to " + host + "." << endl << endl;
}
else
{
cout << "Couldn't connect to " + host + "." << endl << endl;
}
cout << "[Y]es [N]o -> ";
cout << "Ping another host: ";
cin >> ping_again;
if (ping_again == "Y" || ping_again == "y")
{
system("cls");
Ping();
}
else
{
system("exit");
}
}
void main()
{
Ping();
}[/highlight]
Quote Originally Posted by Hassan View Post
[highlight=c]#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
static string host;
static string ping_again;
void Ping()
{
cout << "Enter IP you wish to ping: ";
cin >> host;
int result = system (("ping " + host+ " > nul").c_str() );
if (result==0)
{
cout << "Connected successfully to " + host + "." << endl << endl;
}
else
{
cout << "Couldn't connect to " + host + "." << endl << endl;
}
cout << "[Y]es [N]o -> ";
cout << "Ping another host: ";
cin >> ping_again;
if (ping_again == "Y" || ping_again == "y")
{
system("cls");
Ping();
}
else
{
system("exit");
}
}
void main()
{
Ping();
}[/highlight]
Hmm I logged off for the night, but this worked flawlessly. ^_^ Thanks again, life saver.


EDIT: Can I ask what the .c_str() means?
Quote Originally Posted by BobFM View Post
Hmm I logged off for the night, but this worked flawlessly. ^_^ Thanks again, life saver.


EDIT: Can I ask what the .c_str() means?
NP.

c_str generates same content in the string representation as the expression that called it, but returns it as a pointer to an array of characters.

/Marked Solved.
Quote Originally Posted by Hassan View Post


NP.

c_str generates same content in the string representation as the expression that called it, but returns it as a pointer to an array of characters.
Ah, sounds useful. Last question, can I set the amount of retries to ping the IP? I was going to try a while loop and if it isn't complete pinging, would move on to a new function and execute the "failed" statement. ( if (result==1) )

EDIT: Didn't see your mark solved..sorry bout that.
Quote Originally Posted by BobFM View Post
Ah, sounds useful. Last question, can I set the amount of retries to ping the IP? I was going to try a while loop and if it isn't complete pinging, would move on to a new function and execute the "failed" statement. ( if (result==1) )

EDIT: Didn't see your mark solved..sorry bout that.
Yup. You can. That's where Result variable becomes useful:

[highlight=c]#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <cstdio>
#include <string>
using namespace std;
static string host;
static string ping_again;
void Ping()
{
cout << "Enter IP you wish to ping: ";
cin >> host;
int NumberofRetries =5;
int result ;
result = system (("ping " + host+ " > nul").c_str() );
for (int i = 1 ;i <= NumberofRetries; i++)
{
if (result != 0)
{
cout << endl << "Retrying... [" << i << "/" << NumberofRetries << "]";
result = system (("ping " + host+ " > nul").c_str() );
if (result==0)
break;
}
}
if (result==0)
{
cout << endl << "Connected successfully to " + host + "." << endl;
}
else
{
cout << endl << "Couldn't connect to " + host + "." << endl << endl;
}
cout << "[Y]es [N]o -> ";
cout << "Ping another host: ";
cin >> ping_again;
if (ping_again == "Y" || ping_again == "y")
{
system("cls");
Ping();
}
else
{
system("exit");
}
}
void main()
{
Ping();
}[/highlight]

The original question is answered. That's why it is marked solved. You may ask more related questions. No problem in that.
Quote Originally Posted by Hassan View Post


Yup. You can. That's where Result variable becomes useful:

[highlight=c]#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <cstdio>
#include <string>
using namespace std;
static string host;
static string ping_again;
void Ping()
{
cout << "Enter IP you wish to ping: ";
cin >> host;
int NumberofRetries =5;
int result ;
result = system (("ping " + host+ " > nul").c_str() );
for (int i = 1 ;i <= NumberofRetries; i++)
{
if (result != 0)
{
cout << endl << "Retrying... [" << i << "/" << NumberofRetries << "]";
result = system (("ping " + host+ " > nul").c_str() );
if (result==0)
break;
}
}
if (result==0)
{
cout << endl << "Connected successfully to " + host + "." << endl;
}
else
{
cout << endl << "Couldn't connect to " + host + "." << endl << endl;
}
cout << "[Y]es [N]o -> ";
cout << "Ping another host: ";
cin >> ping_again;
if (ping_again == "Y" || ping_again == "y")
{
system("cls");
Ping();
}
else
{
system("exit");
}
}
void main()
{
Ping();
}[/highlight]

The original question is answered. That's why it is marked solved. You may ask more related questions. No problem in that.
I noticed I accidentally put while. I meant for, and this is exactly what I needed. I'll mess around until I get everything right. /req close
Quote Originally Posted by BobFM View Post
I noticed I accidentally put while. I meant for, and this is exactly what I needed. I'll mess around until I get everything right. /req close
Sounds cool.

/Marked Solved.
/Closed on Request.
recursion : o
Quote Originally Posted by 'Bruno View Post
recursion : o
Why so shocked tho ?
Quote Originally Posted by Hassan View Post

Why so shocked tho ?
cause it is underrated here at mpgh
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us