problem with trying to print output

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <stdio.h>
using namespace std;
int maint(){
	int num1;
	cout << "Enter a number:";
	cin >> num1;
	
	if(num1 == 1){
		fprintf(stdprn,"This is a test to print %s", "string");
		return 0;
	}
}


i'm trying to print the output "This is a test to print" and link it to printer..but the error occur....someone helps me fix this...
Error	1	error C2065: 'stdprn' : undeclared identifier
stdprn isn't part of standard C. Some compilers (don't know which) that are tightly coupled with the OS seem to offer this, but it doesn't look like your compiler is one of them.
why don't you try.
That's in c++
#include <iostream>
if(num == 1)
cout << "This is a test to print "<<num1;


In C
#include<stdio.h>
if(num == 1)
printf("This is a test to print : %d", num);
Last edited on
well...i'm trying to link it to the printer and print out the output....
Lance -

As I mentioned above, stdprn isn't part of C++ (or even C). It was a language "add-on" done by one of the compiler makers years ago, and will only work with their compiler. The fact that you're including <stdio.h> and it's still undeclared, is strong evidence that your set-up doesn't support stdprn.

Standard C/C++ is designed to produce highly portable code. As such, it doesn't assume anything about your system or devices; this is why stdprn capability doesn't exist in most implementations.

So, you have a few choices:

1. write the output to a file, and print the file after your program exits. This approach isn't very automated, but it should result in the most portable code.

2. alter your program to open a stream to your printer device. I can't give you much help on this, so some googling may help you.

3. write the output to a file, and at the end of your program, use a system() call to print the file, then delete the file. This is really not recommended, as the system() call parameters will result in highly unportable code. system() is also an "expensive" function (in terms of CPU) but that probably doesn't matter if you're on a PC.

Let us know what you think of these options, after you look at them a bit.
well...i would like to choose option 2 and implement it in my project..but so far...i can't really find the way to connect to my printer port...this is the sample code...but i don't know whether it work or not...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
 int main()  {  
    cout << "Initialising..." << endl; 
    ofstream print; 
    print.open("lpt1:", ios::out); 
    cout << "Stream opened, sending text..." << endl; 
    print << "Hello World" << endl; 
    for(int i = 0; i < 6; i++) 
    { 
       print << i << endl; 
    } 
    print << "f"; 
    print.close(); 
    return 0; 
 } 
I can't tell you whether it will work or not. I'd need to know more about your printer, and I'm really not the best guy to tackle those questions anyway. If you're using a modern printer, though, I don't know if they accept simple text and just print it out, or if they require a bunch of set-up codes and stuff.

Regarding your code above, it looks good, but you may want to check the fail bit after your open() call, to make sure the stream at least opened.

I don't think I'm going to be much help to you from here on out, but good luck and keep us posted.
C++ knows nothing of printers. Nothing. It also knows nothing about keyboards, monitors or the mouse. Everything to do with that requires some kind of interaction with your OS. What OS are you using?

stdprn was an MS-DOS stream. I guess you're not using MS-DOS. This used to be a Win32 equivalent:

1
2
3
4
5
6
7
8
9
   #include <cstdio>
   int main()
   {
      FILE *stream;

      stream = fopen("PRN", "w");
      fprintf(stream, "some text\n");
     return 0;
   }


I've no idea if it still works.
Topic archived. No new replies allowed.