Topic awaiting preservation: C++ pass variables to system() |
|
---|---|
Author | Thread |
Maniac (V) Inmate From: there...no..there..... |
posted 03-12-2006 14:08
I have a small C++ console app that I need to pass variables to the system() function. Below is what I have but it doesn't work. It just keeps looping through the cin and cout. code: #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string printer, file, sendstring; cout << "Please enter the last three digits of the copiers IP address: "; cin >> printer; cout << "Please enter the name of the file as well as the extension: "; cin >> file; sendstring = "lpr -S 192.1.50." + printer + " " + "-P Printer " + file; system(sendstring.c_str()); system("PAUSE"); return EXIT_SUCCESS; }
|
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 03-12-2006 17:17 |
Maniac (V) Inmate From: there...no..there..... |
posted 03-12-2006 21:11
yeah, it will compile without errors but it does not send the string to the console. I tried the exec() but compiler did not know what that function was. I'll look into that more. |
Paranoid (IV) Inmate From: Madison, Indiana, USA |
posted 03-12-2006 21:27 |
Paranoid (IV) Mad Scientist From: Omicron Persei 8 |
posted 03-12-2006 22:27 |
Maniac (V) Inmate From: there...no..there..... |
posted 03-12-2006 22:53
hyperbole: Yeah, I know. Once you put in the IP and then the file name, it just goes back to asking for the IP and file again. Not sure what's up with that. code: system(sendstring.c_str());
|
Paranoid (IV) Inmate From: Madison, Indiana, USA |
posted 03-13-2006 19:01 |
Maniac (V) Inmate From: there...no..there..... |
posted 03-13-2006 19:05
yeah I tried that. It just prints the line to the console. Doesn't actually execute the command. |
Maniac (V) Inmate From: there...no..there..... |
posted 03-14-2006 03:16
well I figured it out. For some reason you can not use a string literal (I think that is what it's called) and a variable when using the system() function. At least for lpr command anyway. |
Maniac (V) Inmate From: |
posted 03-14-2006 11:33
For the record, I've totally missed out on the resolution of this issue, but... code: "echo lpr -S 192.1.50." + printer + " " + "-P Printer " + file;
|
Paranoid (IV) Inmate From: Madison, Indiana, USA |
posted 03-14-2006 19:41
Actually _Mauro, I use "string " + string + " string" all the time in C++. That is probably because I use the Qt string class and it has overloaded the + operator to allow string concatenation in this way. I had assumed that CPrompt was using a similar class, since he had declared the values to be strings, not char*. But, under normal circumstances, I would say you're correct. |
Maniac (V) Inmate From: |
posted 03-14-2006 20:12
Yeah, dug further and the operator overload is valid in c, but still not recommended for cpp. |
Maniac (V) Inmate From: |
posted 03-14-2006 20:31
No. I just said some big time idiocy. |
Paranoid (IV) Inmate From: Mexico |
posted 03-15-2006 06:03
Yeah, the String class include in the Standard Template Library has the overloaded '+' for concatenation. It is much "safer" according to nearly every STL whore you ask :P |
Maniac (V) Inmate From: there...no..there..... |
posted 03-16-2006 03:29
Well I used the + and it seemed to work fine. I actually went a different route and wrote a VB app to make it more "gui" Printshop likes it better that way. |