Hi Everone,

I need Help With My code If there is any mistake Please Let me Know I will correct
it it is a ping program it will ping an ip address or an address like www.daniweb.com

#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
    int choice;
    int IP;
    int Address;
    
    cout<<"Welcome To Hayzam's Ping"<<endl;
    cout<<"Type any one of Thease"<<endl;
    cout<<"1 or 2"<<endl;
    cout<<"Hint 1:Ping with an IP"<<endl;
    cout<<"Hint 2:Ping with an Address 'Eg www.google.com'"<<endl;
    cin>>choice;
    if(choice == 1)
    {
     cout<<"Give Me The IP"<<endl;
     cin>>IP;
     system("ping IP");
    }
    if(choice == 2)
    {
    cout<<"Please Give Me The IP"<<endl;
    cin>>Address;
    system("ping Address");
    }    
    getch();
    
}

The variables IP and Address are not strings, like they should be, they are integers. You should make them cstrings or std::strings. Also, on lines 22 and 28, you are just calling the system() function with "ping IP" and "ping Address", it does not actually replace "Address" and "IP" with the value that the user inputed.

I would recommend that you make IP and Address std::strings and that you get the input with the getline command like this:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string IP;
	
	cout << "Please enter the IP" << endl;
	getline(cin, IP);
}

This means that you will have to call the std::string member c_str() to pass the string you got from the input into the system() function.

Edited 5 Years Ago by chrjs: n/a

My suggestion (while maybe not the best, but is simple), is:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  const string ping_command = "PING ";
  string input;
  cout << "Enter IP: ";
  getline(cin, input);//<-- gets a line of input into "input"
  string command = ping_command + input;
  //use the command.c_str().
}

.c_str() will return the C style string data the string has. It is required to use the system() function.

This article has been dead for over six months. Start a new discussion instead.