Solved i want to add a variable within the system()

May 22, 2011 at 14:36:53
Specs: N/A
firstly im a beginner ,trying to learn c++ (been approx 10 days) i figured the best way to learn is to try and make a small ping program, anyway i was creating a menu option where a user is prompted to enter a ip address using cin>>gethostid; how can i run a variable within a system() . code looks like this:

case 2: // perform ping
cout<<"Enter IP: \n" <<endl;
cout<<" IP is " << gethostid <<endl;

system("ping -c 4");
cout<<"\n!!!!!!!!!!!!!Ping complete!!!!!!!!!!!!!\n" <<endl;
the code runs fine but i want to add cin>>gethostid in the following line
system("ping " cin>>gethostid);

something like that or am i totally off the mark ?
would be very grateful for any help

many thanks

See More: i want to add a variable within the system()

Report •

May 22, 2011 at 14:46:16
One way may be something like

... // other bits of code

char buffer[128] = { 0 };
sprintf(buffer, "ping %data_type -c 4", variable_name);

Report •

May 22, 2011 at 16:02:25
✔ Best Answer
Since we're using C++, we might as well use C++
std::string str;
std::cout << "Enter IP: \n" <<std::endl;
std::getline(std::cin, str);
str = "ping " + str;

(Don't forget to #include <string>)

How To Ask Questions The Smart Way

Report •
Related Solutions

Ask Question