SlideServe
Menu
Préférences
© SlideServe · v2026

Chapter 4 INPUT AND OUTPUT OBJECTS

mikasi avatar
19 diapositives · 393 Views · Oct 24, 2014

Chapter 4 INPUT AND OUTPUT OBJECTS. Chapter 4. The cout Object The cout object is used to display information to the monitor and is contained in the iostream.h and iostream header files. Chapter 4.

Chapter 4 INPUT AND OUTPUT OBJECTS - slide 1
01 / 19Chapter 4 INPUT AND OUTPUT OBJECTS
1 / 17

Télécharger la présentation

Chapter 4 INPUT AND OUTPUT OBJECTS
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Chapter 4INPUT AND OUTPUT OBJECTS

  2. Chapter 4 • The cout ObjectThe cout object is used to display information to the monitor and is contained in the iostream.h and iostream header files.

  3. Chapter 4 • Using the cout Objectcout << item #1 << item #2 << item #3 << ·· << item #n;

  4. Chapter 4 • The cout Output Stream Flows to the System Monitor

  5. Chapter 4 • Using \n and endl

  6. Chapter 4 • The C++ Escape Sequences Used with cout \a Beep \b Backspace \n CRLF \r CR \t Horizontal tab \v Vertical tab \\ Backslash \' Single quote \” Double quote \? Question mark

  7. Chapter 4 • I/O Stream Manipulatorssetw(n) Sets field width to nsetprecision(n) Sets floating-point precision to nsetfill(n) Sets fill character to nws Extracts white space charactersendl Inserts a new line in the output stream, thenflush Flushes the output stream

  8. Chapter 4 • Setting the Field Widthcout << setw(field width) << output item;

  9. Chapter 4 • Generating Decimal Point Valuescout.setf(ios::fixed );cout.precision(n);

  10. Chapter 4 • Generating Left-Justified Values cout.setf(ios::fixed);cout.setf(ios::left);

  11. Chapter 4 • Generating Currency Valuescout.setf(ios::fixed);cout.setf(ios::showpoint);cout.precision(2);

  12. Chapter 4 • Formatting Functions and Flagssetf() Sets a formatting flag. unsetf() Unsets a formatting flag. precision(n) Sets decimal output to n decimal places. fixed Forces fixed decimal point output. left Forces left justification within field. right Forces right justification within field. scientific Forces exponential, e, output. showpoint Forces a decimal point to be displayed along with trailing 0’s.(Used for currency outputs) showpos Forces a + sign output for positive values.

  13. Chapter 4 • The cin ObjectThe cin object is used to read information from the keyboard and is contained in the iostream.h and iostream header files.

  14. Chapter 4 • The cin Input Stream Flows from the Keyboard

  15. Chapter 4 • Using the cin Objectcin >> variable to be read; • Only one character is read at a time. • White space (blanks, tabs, new lines, carriage returns, etc.) are ignored by cin when using the >> operator. However, white space can be read using different cin functions. • Numeric values can be read as characters, but each digit is read as a separate character.

  16. Chapter 4 • Using get() and put() For Single Character Datacin.get(character variable); cout.put(character variable);

  17. Chapter 4 • Reading Strings Using getline()getline(input stream, string object, 'delimiting character’); • To avoid a potential problem, use the linecin >> ws before the cin.getline() statement.

  18. Chapter 4 • Opening an Input Fileifstream fin(”file path"); • Opening an Output Fileofstream fout(”file path"); • Opening an Input/Output Filefstream finout(“file path”);

  19. Chapter 4 • Processing an Open Fileif(!fileObject) { cout << "This file cannot be opened" << endl; exit(1); }//END IF else { while (Read Data Item From Input File ) { //BEGIN LOOP Process data item. } //END LOOP }//END ELSE