User input in a system("wget")

Currently I'm attempting to get a webpage that the user inputs using a system call. However I can't get the system call with the user's inputted website working.

My program looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream>
  #include <string>
  int system(const char *command);

    int main()
   {
      string website;
      cout<<"Enter a website that you want to grab: ";
      cin>>website;
  
      int get=system ("wget "+website);
      return get; 
   }



Desired output:
Enter a website that you want to grab: www.yahoo.com

With the index.html of yahoo put in the directory the program is being run in.


Any help would be greatly appreciated.
Last edited on
What is the purpose of this line?
 
int system(const char *command);


Tell me more about your problem. Is it not compiling? If it compiled correctly, what happens when you run it? Does it crash, hang, or just display nothing?
It seems as though system() won't accept a string as an argument- you must supply a pointer to a const char. That's fine- we'll use c_str() to convert the string to a c-style string, and then cast it into a const char*.

1
2
3
4
5
6
7
8
int main()
{
	std::string website;
	std::cout<<"Enter a website that you want to grab: ";
	std::cin>>website;
	std::string command = "wget " + website;
	system((const char*)command.c_str());
}


Don't forget your "std::"s, also.
There's no reason to cast command.c_str() to a const char*, it already of that type, this would be fine:

 
system(command.c_str());


Also, you should not declare system(const char*) yourself, but include <cstdlib> instead.
Topic archived. No new replies allowed.