Can someone help me, I've been trying forever and still can't get it. I need to get my C++ program to print in dos enviroment. I need it to print the words "Hello" when i push something and nothing else.

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;

int main(int argc, char *argv[])
{

cout<<"hello";
Return_0;

Dani AI

Generated

Clarifying note: the original posts mix two different things — writing to the console (what cout does) and sending a job to a real printer (paper). wanted the latter; showed a console pause (useful if you only want to wait for a key), and hinted at redirecting output to the parallel port. Below are practical, safe options that work in old DOS or in Windows when you need a literal hard copy.

A quick diagnostic (works from a DOS prompt): test the printer/port first with the shell so you know the hardware/port is OK:
echo Hello > LPT1
or
copy file.txt LPT1
If those print, C++ can simply open that device and write to it. Example using the C++ iostream library to write raw text to LPT1:

#include <fstream>
#include <iostream>

int main()
{
    std::ofstream prn("LPT1");
    if (!prn) { std::cerr << "Cannot open LPT1\n"; return 1; }
    prn << "Hello\r\n";
    return 0;
}

On NT-based Windows (2000/XP and later) the spooler often intercepts printing; you may need Win32 calls for a reliable raw write. A minimal raw-write example uses CreateFile/WriteFile on the device name "\\.\LPT1". If you want fully formatted output or support for modern printers, use the Win32 spooler APIs (StartDocPrinter/WritePrinter) or ShellExecute with the "print" verb.

Troubleshooting/cautions: make sure the printer is actually on LPT1, use CR+LF ("\r\n") and/or a form-feed if the printer needs it, and test the simple command-line redirect before coding. If the target is Windows and you do not control the machine, spooling or driver behavior may prevent direct port writes — in that case create a text file and invoke the system/WinAPI print mechanisms. For the original small trigger behavior, call a key-wait (for example getch() from <conio.h>) and then run the print-code above.

Recommended Answers

All 7 Replies

Something similar to this?

#include <iostream>
using namespace std;

int main()
{
   cout << "Press [enter] to continue...";
   cin.get();
   cout << "Hello\n";
   return 0;
}

Ya but how do i get it to print hello on paper.

>Ya but how do i get it to print hello on paper.
Start by being more specific about your compiler and OS. To do it "right" is a lot of work, but most of the time you can do it pretty easily with a compiler/system-specific solution.

Narue,
What are you talking about professors know everything.......

If you have Windows and Dev-C++ then you are in luck. Look at the code snippet:
http://www.daniweb.com/code/snippet122.html
This might be a little overkill since it allows you to set the font, wordwrap and more. Trim it back to what you need!

In DOS, I'd consider issuing:
ctty lpt1

(Warning! This may be written incorrectly. It's been a while.)

Then, everything would print out at the printer connected to the parallel port. (Great for a paper log of a process.)

Thanks guys , and thanks for that huge ass code on how to print, im still new at c++ and that code didnt really help too too much but i really appreaciate it guys. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.