Tactical Shrimp
Tactical Shrimp

Reputation: 23

How can I change certain parts of a command in the system function in C++

So I have this code. It's very simple

#include <iostream>
using namespace std;

int main() {

string inputfile = "input.pdf";
string outputfile = "output.tiff";
cout << "paste path of pdf to convert" << endl;
cin >> inputfile;
cout << "type the path of the output file with the correct extension ie png jpeg or tif" << endl;
 cin >> outputfile;

system("gm.exe montage -tile 1x10000 -geometry 100% -density 200x200 input.pdf -quality 100 output.tif");

return 0;
}

So I want the user to change the two strings to whatever file path they need and put that into the command instead of input or output file. Is this even possible?

Upvotes: 1

Views: 66

Answers (1)

Matt Coats
Matt Coats

Reputation: 332

string inputfile = "input.pdf";
string outputfile = "output.tiff";
cout << "paste path of pdf to convert" << endl;
getline(cin, inputfile);
cout << "type the path of the output file with the correct extension ie png jpeg or tif" << endl;
getline(cin, outputfile);
string command = "gm.exe montage -tile 1x10000 -geometry 100% -density 200x200 " + inputfile + " -quality 100 " + outputfile;

system(command.c_str());

Note that I now use getline() to gather input from the user -- I've just heard this is a better method, so I changed it here. Then I created a new string variable and concatenated the inputfile and outputfile strings to it to create the command string. Since the system() function requires a const char* as its parameter, I then use c_str() to convert the string to a const char*.

Please not that I have not tested this code, but the idea I've presented here should work.

Upvotes: 2

Related Questions