user3417785
user3417785

Reputation: 81

entering a string into the system function

I want to make a program in C++ that prompt the user to enter a web address and then performs the ping command in the command prompt on that address. In the code, I used the cin command to get the web address into a string variable called n and add it to "ping" in the same variable but, I cannot enter the string into the system function. Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    string ping;
    string n;
    cout << "Enter the link you want to ping ";
    cin >> ping;
    n = "ping "+ping;
    cout << n;
    system(n);
    return 0;
}

How can I enter the string from the variable n into the system function?

Upvotes: 1

Views: 158

Answers (1)

billz
billz

Reputation: 45420

system is a C function,

int system( const char *command );

it doesn't know c++ std::string, you could pass const char* to it:

update

system(n);

to

system(n.c_str());

Upvotes: 2

Related Questions