C++ program to use console for input and printer for output

Need OS and printer connection independent code example

Page 1 of 1

2 Replies - 8560 Views - Last Post: 25 February 2008 - 02:02 PM Rate Topic: -----

#1 Migs  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 09-February 08

C++ program to use console for input and printer for output

Post icon  Posted 13 February 2008 - 01:04 PM

Friends:

I have searched far and wide, also Googled beyond Dream In Code, to no avail. This is what I want to learn to do:

Use my console for input and use my printer and my console for my output of simple text.

All I am looking for is a simple "hello world" level C++ program to illustrate to me how to do this.

I dont want snippets or or references to my OS, as I could hardly understand how to incorporate them into the code. It must use standard C++ usable in any machine.
I'd like something in C++ that prints to the current printer, no matter what its connection to the computer. (USB, Ethernet etc...)

Remember that I' a newbie, with some Basic and Pascal experience, and in Basic at least, all it took was a different print command for the printer (lprint instead of print).

From what I've seen, no one out there has even come close to showing a newbie how to do this. (Thinly veiled challenge)

Thanks amigos,

Migs

Is This A Good Question/Topic? 0
  • +

Replies To: C++ program to use console for input and printer for output

#2 schnalf  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 129
  • Joined: 09-February 08

Re: C++ program to use console for input and printer for output

Posted 13 February 2008 - 01:09 PM

i don't know, but i don't think that there is such a function which is os independent existing.
Was This Post Helpful? 0
  • +
  • -

#6 Migs  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 09-February 08

Re: C++ program to use console for input and printer for output

Posted 25 February 2008 - 02:02 PM

Here I submit one that works to USB and ethernet printers, at least on mine: - Migs


// an example of printing in C++ to the console and a printer
// Thanks etresoft!
// by Miguel Reznicek mreznicek@pretensa.com
// Prints to local and networked printers
// Just make sure you printed to the printer you want to print
// just before this one is printed. (That chooses the printer)

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
// Some sample data including a prompt for a string (name)
int i=1234;
char name[15];
float fval=123.456;
FILE * printer_pipe = popen("/usr/bin/lpr", "w");
cout << "Enter a name: ";
cin >> name;

// Output to the console
// First a couple blank lines to the console;
cout << endl << endl;
// Second a string to the console
cout << "a string name: " << name << endl;
// Third a decimal integer to the console
cout << "A decimal integer: " << i<< endl;
// Fourth a floating pont number to the console
cout << "A floating point number: " << fval<< endl;

// output to the printer
// %d = A decimal integer
// %f = A floating point number
// %c = A Character
// %s = A string
//
// First a couple blank lines to the printer:
fprintf(printer_pipe, "\n \n");
// Second a string (the name) to the printer:
fprintf(printer_pipe, "A string name: %s \n", name);
// Third a decimal number to the printer:
fprintf(printer_pipe, "A decimal integer: %i \n", i);
// Fourth a floating point number to the printer
fprintf(printer_pipe, "A floating point number: %f \n", fval);
// Last a couple blank lines to the printer:
fprintf(printer_pipe, "\n \n", name);
// Closing the printer
pclose(printer_pipe);
return 0;
}
Was This Post Helpful? 1

Page 1 of 1