1 / 17

Chapter 3. Expressions and Interactivity

Chapter 3. Expressions and Interactivity. CSC125 Introduction to C++. Formatting Output. Ex. Input 6 integers n1,n2,n3,n4,n5,n6 in the range of [0,10000], and display them on the screen as a table shown below: n1 n2 n3 n4 n5 n6

kevyn
Télécharger la présentation

Chapter 3. Expressions and Interactivity

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. Chapter 3. Expressions and Interactivity CSC125 Introduction to C++

  2. Formatting Output • Ex. Input 6 integers n1,n2,n3,n4,n5,n6 in the range of [0,10000], and display them on the screen as a table shown below: n1 n2 n3 n4 n5 n6 • Cout uses just the number of spaces needed to print each number

  3. Formatting output • A stream manipulator • setw(aNumber) : establish print fields of a specified width. • aNumber is the field width for the value immediately following it. Minimum number of spaces to print the value in.

  4. Formatting Output • #include <iomanip> • Cout goes back to its default method of printing • Any number larger than the minimum will cause cout to override the setw value.

  5. Formatting Output • The field width of a floating-point number includes a position for the decimal point • The field width of a string includes all characters in the string, including spaces • These values printed in the field are right-justified by default.

  6. Formatting Output • Right justified. double x=146.789, y=24.23,z=1.2; cout<<setw(10)<<x<<endl; cout<<setw(10)<<y<<endl; cout<<setw(10)<<z<<endl; • Left justified. double x=146.789, y=24.23,z=1.2; cout<<left<<setw(10)<<x<<endl; cout<<setw(10)<<y<<endl; cout<<setw(10)<<z<<endl;

  7. The setprecision(aNumber) Manipulator • Floating-point values may be rounded to a number of significant digits • You can control the number of significant digits with which floating-point values are displayed. • Unlike setw(), setprecision manipulator remains in effect until it is changed to some other values.

  8. Fixed vs Scientific • Stream manipulator, fixed, forces cout to print the digits in fixed-point notation. float f1=3.2472, f2=12.9e3; double d=327.815; cout<<fixed<<f1<<endl<<f2<<endl; cout<<scientific<<d<<endl<<f2<<endl; cout<<setprecison(2); cout<<fixed<<f1<<endl<<f2<<endl; cout<<scientific<<d<<endl<<f2<<endl;

  9. Fixed vs Scientific • setprecision(x): when used with fixed or scientific, print floating-point value using x digits after the decimal. Without fixed or scientific, print floating-point value using x significant digits

  10. showpoint • By default, floating-point numbers are not displayed with trailing zeroes • Six significant digits are specified. • showpoint manipulator

  11. Happy Moon Festival

  12. Formatted Input • Can format field width for use with cin • Useful when reading string data to be stored in a character array: const int SIZE = 5; char fName[SIZE]; cout << "Enter your name: "; cin >> setw(SIZE) >> fName; • cin reads one less character than specified in setw() directive

  13. Procedural and Object-Oriented Programming • Procedural programming: focus is on the process. Procedures/functions are written to process data. • Object-Oriented programming: focus is on objects, which contain data and the means to manipulate the data. Messages sent to objects to perform operations.

  14. cin.getline() • To read an entire line of input, use cin.getline(): const int SIZE = 81;char address[SIZE]; cout << "Enter your address: "; cin.getline(address, SIZE); • cin.getline takes two arguments: • Name of array to store string • Size of the array

  15. Reading a charcter • To read a single character: • Use cin: char ch; cout << “Press any key to continue"; cin >> ch; Problem: will skip over blanks, tabs, <CR> • Use cin.get(): cin.get(ch); Will read the next character entered, even whitespace

  16. Mixing cin >> and cin.get() • Mixing cin and cin.get() in the same program can cause input errors that are hard to detect

  17. How to solve it? • To skip over unneeded characters that are still in the keyboard buffer, use cin.ignore(): cin.ignore(); // skip next char cin.ignore(n, c); // skip the next // n char. or until a //character c is met.

More Related