Presentation on theme: "Chapter 3. Expressions and Interactivity CSC125 Introduction to C++"— Presentation transcript:
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 n1 n2 n3 n4 n5 n6 n4 n5 n6 Cout uses just the number of spaces needed to print each number
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.
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.
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.
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.
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<
"name": "Fixed vs Scientific Stream manipulator, fixed, forces cout to print the digits in fixed-point notation.",
"description": "float f1=3.2472, f2=12.9e3; double d=327.815; cout<
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
showpoint By default, floating-point numbers are not displayed with trailing zeroes Six significant digits are specified. showpoint manipulator
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
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.
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
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
Mixing cin >> and cin.get() Mixing cin and cin.get() in the same program can cause input errors that are hard to detect
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.
Your consent to our cookies if you continue to use this website.