C++ Program Help

Put your programming skills to the test in these challenges.

C++ Program Help

Post by 323LORDofWAR323 on Tue Mar 12, 2013 11:20 pm
([msg=74485]see C++ Program Help[/msg])

I am programming in C++

The function of the program is the user puts in the computer name and the printer name, and it adds the printer as a new printer.

I created 2 string variables
a and b
printer and computer

simply running a cout statement the following works:
cout<< "rundll32 printui.dll,PrintUIEntry /if /b "<<a<<" /f %windir%\inf\ntprint.inf /r" <<b<<" /m "<<a<<"";

BUT to make this work to add a printer obviously a system(" ") command must be used
system("rundll32 printui.dll,PrintUIEntry /if /b <<a<< /f %windir%\inf\ntprint.inf /r <<b<< /m <<a<<");

After user inputs a and b the program runs
but the output says "<< was unexpected here"
If i break the quotes like so:
system("rundll32 printui.dll,PrintUIEntry /if /b" <<a<< "/f %windir%\inf\ntprint.inf /r " <<b<< "/m" <<a<<"");
the program wont compile. Obviously this format is incorrect for a system command

So how do i escape the quotation marks?
I know its probably a backslash or a set of 'single quotes' somewhere, but everything i try doesn't work

Thanks for any help :mrgreen:
New User
New User
Posts: 8
Joined: Fri Mar 08, 2013 7:56 pm
Blog: View Blog (0)

Re: C++ Program Help

Post by pretentious on Wed Mar 13, 2013 3:08 am
([msg=74489]see Re: C++ Program Help[/msg])

so i probably have no idea what i'm talking about but i would suggest putting all the info into a string before you pass it to the system() function.
having a bunch of "<<"'s inside those parentheses doesn't look right
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
Posts: 1110
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)

Re: C++ Program Help

Post by KthProg on Wed Mar 13, 2013 8:31 am
([msg=74493]see Re: C++ Program Help[/msg])

Dude, you're tying to fit a square bolt in a round hole.
Just write a batch script -_-
Look at where your quotation marks are btw.
You cant surround << in quotations, because then << becomes a string rather than a concatenation operator.
90% sure.
User avatar
Posts: 219
Joined: Wed Jan 23, 2013 7:06 pm
Blog: View Blog (0)

Re: C++ Program Help

Post by LoGiCaL__ on Thu Mar 14, 2013 9:57 pm
([msg=74541]see Re: C++ Program Help[/msg])

I believe the issue you are running into is that you need to escape the \ character. This is done by putting it twice \\. Also, you don't want to put the << in the string. Otherwise it won't concatenate the variables. Also, I think you will need some space between the /b & a otherwise windows will throw an error. Not sure what you vars are though.

Code: Select all
std::string outputStr = "rundll32 printui.dll,PrintUIEntry /if /b"  << " " << a << "/f %windir%\\inf\\ntprint.inf /r"  << " " << b << " /m" <<a;

User avatar
Posts: 1078
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)

Re: C++ Program Help

Post by 323LORDofWAR323 on Fri Mar 15, 2013 8:15 pm
([msg=74555]see Re: C++ Program Help[/msg])

New User
New User
Posts: 8
Joined: Fri Mar 08, 2013 7:56 pm
Blog: View Blog (0)

Re: C++ Program Help

Post by -Ninjex- on Fri Mar 15, 2013 8:54 pm
([msg=74557]see Re: C++ Program Help[/msg])

Okay, an obvious error I see:

Code: Select all
std::string outputStr;

You defined a string called outputStr above, and then you incorrectly try to assign a value to that string:

Code: Select all
outputStr = "rundll32 printui.dll,PrintUIEntry /if /b" << " " << a << "/f %windir%\\inf\\ntprint.inf /r" << " " << b << " /m" <<a;

I am not great with c++, and don't know the system call, but your code looks like it could use some work...
To me, it looks like you are trying to get the program to ask you for a computer/printer name, and then it takes those names and runs a system command to change it to the value you put in. I believe there is a more efficient way of writing it.

Why not just take the command and turn it into a function. The function can take a parameter, the parameter being the name that you want to change the computer/printer name to.


I just redesigned it a bit, see if it will work for you. It compiled for me in linux, but I had to change one of the headers. I changed #include <windows.h> to #include <stdlib.h>

I believe you need to make use of the c_str() command here.

[as a side note, you defined b first and then a, and it sort of confused me, so I may have accidentally placed the computer name where the printer name should be, it's as easy as changing their locations in the program if so]
Try this:

Code: Select all
#define _WIN32_WINT 0x0500
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()

        string compName;
        string printName;
        string outputStr1 = "rundll32 printui.dll,PrintUIEntry /if /b ";
        string outputStr2 = " /f %windir%\\inf\\ntprint.inf /r ";
        string outputStr3 = " \m";

        system("color 0a");
        system("set computer");

        cout << "Enter Computer Name: " << endl;
            cin >> compName;
            cout << endl;
        cout << "Enter Printer Name: " << endl;
            cin >> printName;

        string systemcall = outputStr1+compName+outputStr2+printName+outputStr3+compName;

For those that know
K: 0x2CD8D4F9
User avatar
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests