943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 26430
  • C++ RSS
May 23rd, 2004
1

I need special stuff in my project like system("cls")

Expand Post »
hii, i am computer science student and i am doing win console application program "Project" by c++ compiler "mic visiual 6", the project is like a small data base which stores student names and thier grades and search for names, add, delete,edit and other things. i am asking if i can make it more friendly by putting colors or any special stuff. for example i read in a forum ( system("cls") ) which really helped me in my project and system("pause") too. any one know things like that plzzzz tell me. i need to know these stuff.
another thing.. i read that there is another systems like system("time") if any one know what these systems make plzz help me.:rolleyes:
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dooda man is offline Offline
15 posts
since May 2004
May 24th, 2004
0

Re: I need special stuff in my project like system("cls")

Formatting Output
Formatting output is one of the most important aspects of developing user-friendly programs. C++ offers several conventions that allow a programmer to format output on the screen. The functions that deal with output format are in the standard include file "iomanip.h."

Two common I/O manipulators are setw and setprecision. Setw sets the width of the field allocated for the output. It takes the size of the field (in number of characters) as a parameter. For example, the code:

C++ Syntax (Toggle Plain Text)
  1. cout << setw(5) << "H";
  2. generates the following output on the screen (each underscore represents a space):
  3.  
  4. _ _ _ _ H
The setprecision manipulator sets the total number of digits to be displayed when floating point numbers are output. For example, the code:

C++ Syntax (Toggle Plain Text)
  1. cout << setprecision(5) << 123.456;
  2.  
  3. generates the following output on the screen:
  4.  
  5. 123.46
The setprecision manipulator can also set the number of decimal places displayed, but first you have to set an ios flag that specifies that it should behave this way. The flag is set with the following statement:

C++ Syntax (Toggle Plain Text)
  1. cout.setf(ios::fixed);

Once the flag has been set specifying that floating point output should be fixed, the number you pass as a parameter to setprecision is the number of decimal places displayed. For example, the code:

C++ Syntax (Toggle Plain Text)
  1. cout.setf(ios::fixed);
  2. cout << setprecision(5) << 12.345678;
  3.  
  4. generates the following output on the screen:
  5.  
  6. 12.34567
Pausing Output
A frequently encountered problem involves pausing the output of a program so that the output can be read before the output window closes. Use the Execute command on the Build menu to run your program. The Execute command will automatically place a "Press any key to continue..." prompt on the screen when your program terminates. When you have finished with the output, press a key to make the window close.

Clearing the Screen
To clear the entire output window of a program, you must use a system function found in the stdlib.h include file. First, include stdlib.h at the top of your program. Then, just insert the following call whenever you want to clear the screen:

system("CLS");

The program below is an example of how to use this method of clearing the screen.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5.  
  6. {
  7. cout << "Text before the clear screen." << endl;
  8. system("CLS");
  9. cout << "Text following the clear screen." << endl;
  10. return 0;
  11. }
Sending Output to the Printer
How you print is now a function of your operating system. To print using Windows 95/98 or NT, you have to get your output to a print manager in the operating system.

To direct output to a printer, you must first open a file stream to send output to. You can do this using the same method that you use to send output to a file. First, declare a variable of type fstream. Then open the stream using the member function, open. Instead of opening the stream using the name of a file, use the name of your printer port, for example, "LPT1."

Once a file stream is open, you can print using the name of the stream variable followed by the << operator and the data you want to send to the printer. Be sure to close the stream when you no longer need to use it.

The example program below will send text to a printer on port LPT1.

C++ Syntax (Toggle Plain Text)
  1. #include <fstream.h>
  2.  
  3. int main()
  4.  
  5. {
  6. ofstream print; // stream variable declaration
  7. print.open("LPT1"); // open stream
  8.  
  9. // Print Text (the character ‘\f’ will produce a form feed)
  10. print << "This text will print on the printer.\f";
  11. print.close(); // close stream
  12. return 0;
  13. }
Important Note:
1-Blindly directing text to a printer port assumes some things are true.

It assumes that you have a printer attached to the specified port or that the port is being captured and redirected to a printer.
It assumes that the printer is capable of accepting plain text. For example, a PostScript printer will not print unless the data comes through a PostScript printer driver. The code above does not make use of any drivers.
another example this program traping function key F1 and F2
C++ Syntax (Toggle Plain Text)
  1. # include<iostream.h>
  2. # include<conio.h>
  3. void function_key(); //prototype of the function
  4. void main()
  5. {
  6. function_key(); //function call
  7. }
  8. void function_key() //function body
  9. {
  10. int ret;
  11. ret = getch(); //get the ASCII value of the key pressed
  12.  
  13. if (ret == 0)
  14. {
  15. ret = getch()+256;
  16.  
  17. switch(ret) //switch between various values of 'ret'
  18. {
  19. case 315:
  20. cout<<" F1 pressed!\n\n";
  21. break;
  22. case 316:
  23. cout<<"F2 pressed!\n";
  24. break;
  25. case 317:
  26. cout<<"F3 pressed!\n";
  27. break;
  28. case 318:
  29. cout<<"F4 pressed!\n";
  30. break;
  31. case 319:
  32. cout<<"F5 pressed!\n";
  33. break;
  34. case 320:
  35. cout<<"F6 pressed!\n";
  36. break;
  37. case 321:
  38. cout<<"F7 pressed!\n";
  39. break;
  40. case 322:
  41. cout<<"F8 pressed!\n";
  42. break;
  43. case 323:
  44. cout<<"F9 pressed!\n";
  45. break;
  46. case 324:
  47. cout<<"F10 pressed!\n";
  48. break;
  49. default:
  50. cout<<"Non function key was pressed!\n";
  51. break;
  52. }
  53. }
  54. else
  55. cout<<"Non Function Key pressed!\n";
  56.  
  57. }
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
May 27th, 2004
0

Re: I need special stuff in my project like system("cls")

Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 2nd, 2004
1

Re: I need special stuff in my project like system("cls")

Wow!
I really can use this stuff too.
Thank a lot.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
MaxC is offline Offline
11 posts
since May 2004
Jun 7th, 2004
0

Re: I need special stuff in my project like system("cls")

You are most welcome.Now there is a full tut on file i/o in the C++ tutorial section :-)
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: To c++.NET
Next Thread in C++ Forum Timeline: help with an easy code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC