mari
Uploaded by
51 SLIDES
680 VUES
510LIKES

Introduction to Stream Manipulation and Number Systems in C++

DESCRIPTION

This lecture covers fundamental concepts of stream manipulation in C++, focusing on the use of various manipulators like `setw`, `setfill`, `setprecision`, and base setting for number systems (binary, decimal, octal, and hexadecimal). Students will learn how to format output effectively, manage input states using `cin` functions, and apply different numbering systems for better data presentation. Practical code examples illustrate how to implement these manipulators in C++ programming, enhancing their proficiency in handling input and output streams.

1 / 51

Download Presentation
Télécharger la présentation

Introduction to Stream Manipulation and Number Systems in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Lecture 36

  2. #include <iostream.h>#include <fstream.h>

  3. iomanip.h

  4. cin.eof ( ) ; • cin.fail ( ) ; • cin.bad ( ) ; • cin.good ( ) ; • cin.clrear ( ) ;

  5. Manipulators

  6. Stream Manipulators

  7. float PI = 3.1415926 ;

  8. endl

  9. cout << endl ;

  10. cout << flush ;

  11. Manipulator With Arguments

  12. Inline Manipulator

  13. cout.flush ( ) ;

  14. Number System • Binary • Decimal • Octal • Hexadecimal

  15. Example int i = 10 ; cout << i ;

  16. 10

  17. Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 10 ; Output cout << oct << i << endl ; 12 cout << hex << i<< endl ; A cout << dec << i << endl ; 10 }

  18. White Space

  19. WS Manipulator

  20. setw

  21. Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 5 ; cout << “The value of i is = ” ; cout << setw ( 4 ) << i << endl ; }

  22. setfill

  23. cout << setfill ( ‘*’ ) ; A Character

  24. Example #include<iostream.h> #include<iomanip.h> Main ( ) { int i = 4000 ; cout << setfill ( ‘*’ ) << setw ( 10 ) << i << endl ; }

  25. Set Precision Manipulator

  26. Example #include<iostream.h> #include<iomanip.h> main ( ) { float number = 6.67076632 ; cout << setprecision ( 2 ) << number << endl ; }

  27. Example #define PI 3.1415926 main ( ) { cout << PI << endl ; cout << setprecision ( 2 ) << PI << endl ; }

  28. setbase

  29. Example #include <iostream.h> #include <iomanip.h> main ( ) { int x = 10 ; cout << setbase ( 8 ) << x <<endl ; cout << setbase ( 16 ) << x <<endl ; cout << setbase ( 10 ) << x <<endl ; cout << setbase ( 0 ) << x <<endl ; } Same as setbase (10)

  30. Input Output state flags IOS Flags

  31. width ( ) ;

  32. cin.width ( 7 ) ;cout.width ( 10 ) ;

  33. cout.precision ( 2 ) ;

  34. Example #include <iostream.h> #include <iomanip.h> main ( ) { int i = 10 , j = 20 ; cout << setw ( 7 ) << i <<endl ; cout << j ; }

  35. Formatting Manipulation • ios :: adjustfield • ios :: left • ios :: right • ios :: left | ios :: right , ios :: adjustfield

  36. cout.setf ( ios :: left , ios :: adjustfield ) ; Set Flag

  37. cout.fill ( ‘*’ ) ;

  38. cout.fill ( '0' ) ;

  39. Formatting Manipulation cout.fill ( '0' ) ; cout << setw ( 10 ) << number << endl ;

  40. cout.setf ( ios :: hex ) ;

  41. 7ff 0111 1111 1111

  42. showbase

  43. showbase cout.setf ( ios :: showbase ) ;

  44. showbase cout.setf ( ios :: showbase ) ; cout.setf ( ios::dec , ios :: basefield ) ; cout << x << '\n' ; // Outputs 77 cout.setf ( ios :: oct , ios :: basefield ) ; cout << x << '\n' ; // Outputs 077 cout.setf ( ios :: hex , ios :: basefield ) ; cout << x << '\n' ; // Outputs 0x77

  45. ios :: scientific

  46. cout.setf ( ios :: scientific ) ;

  47. Scientific Notation 1.2334e+09 +/-

  48. Fixed Point Notation

  49. ios :: fixed

  50. ios :: uppercase

More Related
SlideServe
Audio
Live Player
Audio Wave
Play slide audio to activate visualizer