Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3. Expressions and Interactivity CSC125 Introduction to C++

Similar presentations


Presentation on theme: "Chapter 3. Expressions and Interactivity CSC125 Introduction to C++"— 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 n1 n2 n3 n4 n5 n6 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. 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. aNumber is the field width for the value immediately following it. Minimum number of spaces to print the value in.

4 Formatting Output  #include  #include  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 Name of array to store string Size of the array Size of the array

15 Reading a charcter  To read a single character: Use cin : Use cin : char ch; cout << “Press any key to continue"; cin >> ch; Problem: will skip over blanks, tabs, Problem: will skip over blanks, tabs, Use cin.get() : 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 // n char. or until a //character c is met. //character c is met.


Download ppt "Chapter 3. Expressions and Interactivity CSC125 Introduction to C++"

Similar presentations


Ads by Google