Closed Thread Icon

Topic awaiting preservation: C++ pass variables to system() (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=27633" title="Pages that link to Topic awaiting preservation: C++ pass variables to system() (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: C++ pass variables to system() <span class="small">(Page 1 of 1)</span>\

 
CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted 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;
}



All I need it to do is ask for the last octet of the IP address and the name of the file, then send the lpr command to the console. Might be silly but that is how we have to print some of these files that are sent to us.

I was going to try to write a GUI app to do this but couldn't get it to work in VB either

Thanks in advance!

Later,

C:\

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 03-12-2006 17:17

well i had to #include <string> to have it compiled, but other than that it seemed to work fine.

instead of system() you can maybe try exec()

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted 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.

Later,

C:\

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 03-12-2006 21:27

Something else must be going on here. You say it keeps looping through your prompts, but there's nothing in that program to make it loop.

.



-- not necessarily stoned... just beautiful.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 03-12-2006 22:27

what OS are you on?
if its win, try #include <process.h> or #include <windows.h> to get the exec(). that should do fine.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted 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.

Grumble: Tried that...no workie

I'm using WinXP and the IDE I use is Dev-C++

Am I sending the string to the console right by doing the :

code:
system(sendstring.c_str());



Later,

C:\

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 03-13-2006 19:01

Try changing this line:
sendstring = "lpr -S 192.1.50." + printer + " " + "-P Printer " + file;
to
sendstring = "echo lpr -S 192.1.50." + printer + " " + "-P Printer " + file;

and see what happens.

.



-- not necessarily stoned... just beautiful.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 03-13-2006 19:05

yeah I tried that. It just prints the line to the console. Doesn't actually execute the command.

Later,

C:\

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted 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.

I had to put all the DOS lpr command in one variable and then send that through system(). It makes a lot of variables but I think I can live with that.

Thanks for all the help!

Later,

C:\

_Mauro
Maniac (V) Inmate

From:
Insane since: Jul 2005

posted 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;



This is Java. String concatenation in C/Cpp should -never- use the + operator, it should use the strcat function.
Why? Because Java, as the VM is a semi-interpreter, can analyse a string at runtime, not C/Cpp compilers.
Cpp compilers, for such a call to work "normally" should be allowed to analyse the string and string length at compile time.

The fact your original code worked on hyperbole and Grumble's station is merely luck, and incorrect compiler behavior (some global dev cpp setting allowing optimisations, or warnings disabled, or some third party string library).

hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted 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.

CPrompt:
Printing the line on the command console was exactly the effect I was looking for. That proved that the system command was working in a visible way.

I'm glad you got it working.

.



-- not necessarily stoned... just beautiful.

_Mauro
Maniac (V) Inmate

From:
Insane since: Jul 2005

posted posted 03-14-2006 20:12

Yeah, dug further and the operator overload is valid in c, but still not recommended for cpp.
It has something to do with the way both operations are performed: strcat appends a string at the end of another, doesn't do mem allocation, and overwrites the terminal '\0'.
'+' does (some other stuff which causes computers to explode).

Gonna check the Stroustrup faq, I've so blindly learnt strcat was the Holy Grail I have to find out why.

_Mauro
Maniac (V) Inmate

From:
Insane since: Jul 2005

posted posted 03-14-2006 20:31

No. I just said some big time idiocy.
I've learnt that in school, btw, and this irks me.

But anyway.
The + operator IS the cpp version.
And strcat, in comparison, is C bullshit. It will do to concatenate strings, but it sucks.

And Stroustrup doesn't recommend it.
Bjarne Stroustrustrup is the inventor of the Cpp language btw.

(off to try his new shotgun at the ass who repeated "the + operator is for Java strings" to me for.. a year or so)

Alevice
Paranoid (IV) Inmate

From: Mexico
Insane since: Dec 2002

posted 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

Btw, it only works on teh String class. By default, C++ does not support primitive string concatenation trhough the + operator.

__________________________________


Sexy Demoness cel

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted 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.

however, i did learn something which is always good.

thanks for all the help!

Later,

C:\

« BackwardsOnwards »

Show Forum Drop Down Menu