help with c++ system() function

I am trying to make an application on the console and I need to be able to take the input from x and add it to the end of a system as command arguments. It is meant to be the back-end for another console application I am making application Any help would be greatly appreciated. So anyway, here's my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>
using namespace std;
string x;
int y;
int main()
{
cout << ("This is the website downloader.");
system("set CURRNETDIR=%CD%");
cin >> x;
system("wget.exe") << x;
cout << ("Press 1 when done");
cin >> y;
if(y==1) return 0;
}
Last edited on
system("wget.exe") << x;
This just makes no sense at all. You're trying to use the << operator on whatever gets returned by the function call system("wget.exe").

Break it down into blocks. You need to assemble the string to pass into the system command first, and then pass it in.

1
2
3
4
string theStringToPassToSystem = "wget.exe";
cin >> x;
theStringToPassToSystem = theStringToPassToSystem + x;
system(theStringToPassToSystem.c_str());


As an aside, using system for this is not great. Your OS provides functions designed for you to use to start other programs. Look into using those instead.
Last edited on
I was trying to use it like:
1
2
cin >> x;
cout <<  "wget" << x;


x being the website entered.
The << operator is well-defined for objects of type ostream (which is what cout is): http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/

It is not defined for objects of type int in this way, which is what the function system returns, and you don't even want to actually apply it to the returned value of the function; you want to apply it to the parameter you're going to pass in to the function.

In short, << is very inappropriate for this :) Applied to an int, it is the bitshift operator. Completely different.
Last edited on
It's fair to say, never use system(). Why don't you just use a batch file for this?
I just want add the input on to the end of the command within the system input is there a simple way to do this?
I just want add the input on to the end of the command within the system input is there a simple way to do this?


Didn't I already answer this? It doesn't get much simpler than this. I'll add comments to explain it.

1
2
3
4
5
6
7
8
9
10
11
12
13
// Make a string. This is the start of what we will pass to system
string theStringToPassToSystem = "wget.exe";

// Get the user input
cin >> x;

// add that to the end of the string we already have
theStringToPassToSystem = theStringToPassToSystem + x;

// pass it to system. system doesn't like proper strings, it likes pointers to char,
//   so we have to use the string function c_str() to make the string into a 
//   pointer to char
system(theStringToPassToSystem.c_str());
Last edited on
sorry i'm just starting out with cpp.
Last edited on
Is it possible todo:
1
2
3
4
string z = "wget.exe";
cin >> x;
z = theStringToPassToSystem + x;
system(z.c_str());


Instead of:

1
2
3
4
string theStringToPassToSystem = "wget.exe";
cin >> x;
theStringToPassToSystem = theStringToPassToSystem + x;
system(theStringToPassToSystem.c_str());
Last edited on
Yes. You can name the variable almost anything you like. It is perfectly acceptable to name a variable z. You missed one, though.

z = z + x;
Thanks, this will actually make things alot easier.
Topic archived. No new replies allowed.