Thread: how do you make a program send data to the printer?

  1. #1
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15

    Post how do you make a program send data to the printer?

    Do you guys know how to make your program print some data? The reason I ask is in our school we have an ANCIENT computer in the weight room that we use to make a lifting schedule based on maxes that we enter in. I have taken it upon myself to write a program that will do the same thing for a newer computer. All attempts to adapt the old program have failed. I need to know how to make my program print a hard copy so we can carry our schedule around with us. Help?
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    system dependant. we need some specs.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    Windows XP SP2, idk what kind of printer the school would use... probably a usb or network printer... maybe serial, but I doubt it.
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    You could just send the output to a .txt file, once that is done and the file stream is closed you can then just make the program launch the txt file with the system command. People can then just easily do file print (and have easy access to all the printer properties they would normally have access to, select diff printers, ect).


    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    int main()
    {
    
    	ofstream out;
    
    	out.open("test.txt");
    
    	out << "Like this";
    
    	out.close();
    
    	system("test.txt");
    
    
    	return 0;
    
    }
    You would want to check for opening failed, ect. But that would be much simpler and allow the user more control than just direct output to printer IMHO.

  5. #5
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    thats an idea. i might just use that. If I understood it...
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Compile and build an exe with this code (and run the exe):
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    int main()
    {
    
    	int reps;
    	int weight;
    	int sets;
    	
    	ofstream out_to_file;
    
    	out_to_file.open("temp.txt");
    
    	cout << "How many sets of leg presses would you like to do? : ";
    	cin >> sets;
    	cout << "How many reps per set?: ";
    	cin >> reps;
    	cout << "And at what weight? : ";
    	cin >> weight;
    
    
    
    
    	out_to_file << "Legpress:: " << sets << " sets at " << weight << " lbs for " << reps << " reps." << endl;
    
    
    	out_to_file.close();
    
    	system("temp.txt");
    
    
    	return 0;
    
    }
    Then all the user has to do is go to the File menu and hit print (or ctrl + P). Just like in any windows program. Just as if you where going to print this webpage, or print a Word file.

  7. #7
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    thats great, but I dont understand exactly what you guys are doing to send the data to a file. sorry, just started programming less then a week ago...
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  8. #8
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    thats great, but I dont understand exactly what you guys are doing to send the data to a file. sorry, just started programming less then a week ago...
    Ohh ok. Why did ya not say so! I thought you just where not sure what I was saying do.
    It is really simple:
    http://www.cprogramming.com/tutorial/lesson10.html

    Then what ever you call your file stream you can use inplace of "cout" and do everything just like you would if it was in the console screen.
    In my example:
    Code:
    ofstream out_to_file;
    
    out_to_file.open("temp.txt");
    Simply declares a file stream called "out_to_file" and links that file stream to a 'temp.txt'. If temp.txt does not exist it creates it, if it does exist it overides all the data in it (or you can append, alll kinds of good stuff).
    Then when I want to chose what I want to output, just like the console and cout but instead of cout I use 'out_to_file' (or whatever you call your output stream)
    Code:
    out_to_file << (stuff I want to output);
    To use filestreams you must
    Code:
    #include <fstreams>
    Last edited by Enahs; 10-19-2005 at 06:36 PM.

  9. #9
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    my compiler cant seem to find fstreams. Here is my code so far. it cant print yet.
    Code:
    #include <iostream>
    
    
    char liftName;
    int Max;
    
    
    
    
    unsigned short int liftingPlanner (char liftName, int Max)
             {
                    std::cout << "Lift: " <<liftName<<"\n";
                    std::cout << "                Sets              \n";
                    std::cout << "Day ||  1    2    3    4    5\n";
                    std::cout << "| 1 || " <<Max*.6<< " "<<Max*.65<<" "<<Max*.7<<" "<<Max*.75<<" "<<Max*.7<<"\n";
                    std::cout << "| 2 || " <<Max*.65<< " "<<Max*.7<<" "<<Max*.75<<" "<<Max*.8<<" "<<Max*.75<<"\n";
                    std::cout << "| 3 || " <<Max*.7<< " "<<Max*.75<<" "<<Max*.8<<" "<<Max*.85<<" "<<Max*.8<<"\n";
                    std::cout << "| 4 || " <<Max*.75<< " "<<Max*.8<<" "<<Max*.85<<" "<<Max*.9<<" "<<Max*.85<<"\n"; 
                    std::cout << "| Max Day! || *Warm up before maxing* || Max Day!|\n";
                    }
    
    
    int main()
    {
          int counter = 0;
          while(counter < 10)
          {    
          std::cout <<"This program was created by Aaron Quamme\n for BHS in October of 2005\n";
          system ("PAUSE");
          
          std::cout <<"Enter the name of your first lift.\n";
          std::cin >> liftName;
          std::cout << "Enter your max:\n";
          std::cin >> Max;
          liftingPlanner (liftName, Max);
          counter++;
          
          return 0;
          }
          system ("PAUSE");
          return 0;
          }
    It compiles fine but liftName cant hold more than a one letter lift name and the program closes on me when liftingPlanner is called. I dont know whats wrong, like i said before i have been programming for less then a week so dont start flaming me because im to lazy to do my own debugging. I'd appriciate any help.
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  10. #10
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    my compiler cant seem to find fstreams.
    Last line of my previous post.
    Also, re-read the tutorial I linked to about file input and output in my previous post then.

    It compiles fine but liftName cant hold more than a one letter
    Char's can only hold one letter unless you use an array.
    Use strings instead.
    http://www.cprogramming.com/tutorial/lesson9.html
    and the program closes on me when liftingPlanner is called
    It is a function that expects to return something.
    You can either add return 0; to the end of the function, or change the declaration to something like this (void means it does not expect to return a value)

    Code:
    void liftingPlanner (char liftName, int Max)
    I am surprised your compiler even let you try and compile and did not warn you.

    like i said before i have been programming for less then a week so dont start flaming me because im to lazy to do my own debugging.
    Ok, but can we flame you for poor communication skills through the written language? Minor attempts at punctuation and capitalization will make it A LOT easier to read your post, and thus will make it more likely somebody will be willing to help.

  11. #11
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    Quote:
    my compiler cant seem to find fstreams.


    Last line of my previous post.
    I used #include to try and find fstreams. My compiler said it couldn't find the file.
    Ok, but can we flame you for poor communication skills through the written language? Minor attempts at punctuation and capitalization will make it A LOT easier to read your post, and thus will make it more likely somebody will be willing to help.
    I apologize. My capitalization and punctuation have been slipping ever since I started using MSN. I will clean my typing up. Thank you for the help! I will let you know how it goes within a few minutes...
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  12. #12
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    wait wait wait, isn't it
    Code:
    #include <fstream>
    ?
    not fstreams
    just fstream without the s... that should help your compiler find fstream
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  13. #13
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    ?
    not fstreams
    just fstream without the s... that should help your compiler find fstream
    Bah me! Well...at least I got it right in the sample code(s) I gave him

  14. #14
    n00b programmer
    Join Date
    Oct 2005
    Posts
    15
    O.K. guys I tried. I really did. But, I can't seem to find the bug in my program. My compiler gives me an error that says "Invalid conversion from "char" to "char" on line 36.
    Code:
    #include <iostream>
    #include <fstream>
    
    
    char liftName[50];
    int Max;
    
    int liftingPlanner (char liftName, int Max)
             {
                    std::cout << "Lift: " <<liftName<<"\n";
                    std::cout << "                Sets              \n";
                    std::cout << "Day ||  1    2    3    4    5\n";
                    std::cout << "| 1 || " <<Max*.6<< " "<<Max*.65<<" "<<Max*.7<<" "<<Max*.75<<" "<<Max*.7<<"\n";
                    std::cout << "| 2 || " <<Max*.65<< " "<<Max*.7<<" "<<Max*.75<<" "<<Max*.8<<" "<<Max*.75<<"\n";
                    std::cout << "| 3 || " <<Max*.7<< " "<<Max*.75<<" "<<Max*.8<<" "<<Max*.85<<" "<<Max*.8<<"\n";
                    std::cout << "| 4 || " <<Max*.75<< " "<<Max*.8<<" "<<Max*.85<<" "<<Max*.9<<" "<<Max*.85<<"\n"; 
                    std::cout << "| Max Day! || *Warm up before maxing* || Max Day!|\n";
                    return 0;
                    }
                    
    
    int main()
    {     
          system ("COLOR 1F ");
          int counter = 0;
          while(counter < 10)
          {    
          std::cout <<"This program was created by not telling you my real name ;) \nfor BHS in October of 2005\n";
          system ("PAUSE");
          
          std::cout <<"Enter the name of your first lift.\n";
          std::cin >> liftName;
          std::cout << "Enter your max:\n";
          std::cin >> Max;
          liftingPlanner (liftName, Max);
          counter++;
          
          return 0;
          }
          system ("PAUSE");
          return 0;
          }
    Home is where the computer is
    I like the smell of the treats!
    Mmmm, Root Beer...

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The first parameter to function liftingPlanner() is a single character, not a character array. If a character array is intended, then change it like this
    Code:
    int liftingPlanner (char  liftName[], int Max)
             {
    or like this
    Code:
    int liftingPlanner (char*  liftName, int Max)
             {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. send data to printer and print on paper
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 07-22-2002, 05:19 AM