Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Formatting & Strings in C++: Controlling Output and Input, Lecture notes of C programming

An overview of formatting output and inputting strings in C++. It covers topics such as setting output width, precision, and justification using manipulators, as well as reading whole lines of text and handling leading whitespace with cin and getline. The document also emphasizes the importance of hand tracing for analyzing and understanding code.

Typology: Lecture notes

2021/2022

Uploaded on 09/27/2022

laskhminaran
laskhminaran 🇺🇸

4.7

(6)

224 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
05/06/11 1
Slides #5
Formatting & Strings
Sections 3.8-3.12
CMPT 125/128
Dr. B. Fraser
05/06/11 2
Topics
1) How can we format the output on the screen?
2) How can we input strings?
3) How can we analyze C++ code without a
computer?
05/06/11 3
Formatting Output
Random Shopping List:
SKU Item # $ per Price
------ ----------------- --- ------ --------
10041 Hammer 8 1.00 8.00
16334 Book on C++ 1 2.25 2.25
29169 Cowboy hat 5 15.35 76.75
21478 USB Memory Stick (8Gig) 19 0.10 1.90
36962 Cellular phone 5 0.90 4.50
15705 Pens (assorted) 6 1.11 6.66
33281 Pizzas (anchovies & cheese) 8 22.00 176.00
19961 Gum 12 34.45 413.40
12995 Hybrid vehicles 3 88.11 264.33
14827 Staples 17 64.73 1100.41
------ ----------------- --- ------ --------
Total 2054.20
05/06/11 4
Formatting Overview
ì
We can control how cout formats output.
:
setw() Set width of output.
:
setprecision() Set number of decimal places.
:
fixed Prevent scientific notation &
forces display of decimal point.
:
left or right Control left vs right justification.
ì
These are all called manipulators:
:
:
Must #include <iomanip>
pf3
pf4
pf5

Partial preview of the text

Download Formatting & Strings in C++: Controlling Output and Input and more Lecture notes C programming in PDF only on Docsity!

1

Slides #5Formatting & StringsSections 3.8-3.12 CMPT 125/128 ^ Dr. B. Fraser

05/06/^

Topics1) How can we format the output on the screen?2) How can we input strings?3) How can we analyze C++ code without acomputer?

3

Formatting Output Random Shopping List:SKU Item #^ $^ per

Price ------ -----------------^

---^ ------^ --------

10041 Hammer^

8 1.00^ 8.

16334 Book on C++^

1 2.25^ 2.

29169 Cowboy hat^

5 15.35^ 76.

21478 USB Memory Stick (8Gig)

19 0.10^ 1.

36962 Cellular phone^

5 0.90^ 4.

15705 Pens (assorted)^

6 1.11^ 6.

33281 Pizzas (anchovies & cheese)

8 22.00^ 176.

19961 Gum^

12 34.45^ 413.

12995 Hybrid vehicles^

3 88.11^ 264.

14827 Staples^

17 64.73^ 1100.
------ -----------------^
---^ ------^ --------

Total^

2054.20^ 05/06/^

Formatting Overview^ ^ We can control how cout formats output.^ ^ setw()^ Set width of output.^ ^ setprecision()^ Set number of decimal places.^ ^ fixed^ Prevent scientific notation &forces display of decimal point.^ ^ left or right^ Control left vs right justification.^ ^ These are all called manipulators:^  ^ Must #include

5

setw()^ ^ setw() is a manipulator:^  ^ Great for lining up data on the screen.^ ^ setw() only affects the one next element.^ ^ Example:cout << "[" << 12 << "]";cout << "[" << setw(5) << 12 << "]";

Pads with spaces whenitem is fewer charactersthan the setw()'s width.if it's larger than width.^ setw.cpp

05/06/^

Making a table#include #include using namespace std;int main() {const int WIDTH1 = 15;const int WIDTH2 = 18;const int WIDTH3 = 12;cout << setw(WIDTH1) << "Name:"<< setw(WIDTH2) << "Fav Food"<< setw(WIDTH3) << "Fav Number"<<endl;cout << setw(WIDTH1) << "Dr. Evil"<< setw(WIDTH2) << "Cupcakes"<< setw(WIDTH3) << "100000000"<<endl;cout << setw(WIDTH1) << "I.L.B. Bach"<< setw(WIDTH2) << "Anchovies"<< setw(WIDTH3) << "1997"<<endl;//..... omitted to fit on slide.return 0;}

Name:^ Fav Food^ Fav NumberDr. Evil^ Cupcakes^100000000 I.L.B. Bach^ Anchovies^

1997 Me^ Pizza and Cake^

(^0) setwTable.cpp 7

setprecision()^ ^ setprecision() displays a floating-point value...const double PI = 3.14159265;cout << PI << endl;cout << setprecision(4) << PI << endl;cout << setprecision(3) << PI << endl;cout << setprecision(2) << PI << endl;cout << setprecision(1) << PI << endl;cout << setprecision(15) << PI << endl;^ ^ Notes:^ ^ setprecision() stays in effect until changed.^ ^ must include

3.141593.142Value is3.14rounded.3.1 3 3.14159265 setprecision.cpp^

05/06/^

fixed ^ fixed does 2 things:1) Prevents scientific notation (like 1.23E8)2) Changes setprecision() to...(not significant digits)const double BIG = 12345678.9;cout << BIG << endl;cout << fixed;cout << BIG << endl;cout << setprecision(2);cout << BIG << endl;

Cost 1: 1.99Cost 2: 3.55Cost 3: 2.10Cost 4: 1.00Cost 5: 1 1.23457e+00712345678.90000012345678.90cout << fixed << setprecision(2);cout << "Cost 1: " << 1.9876 << endl;cout << "Cost 2: " << 3.55 << endl;cout << "Cost 3: " << 2.1 << endl;cout << "Cost 4: " << 1.0 << endl;cout << "Cost 5: " << 1 << endl; Big number defaults toscientific notation.fixed avoids sci. notn.Default precision...fixed + setprecision(2)gives 2 digits after .fixed and setprecision()no effect on integers.fixed.cpp

15

String input^ ^ Read in single words with:string firstName;cin >> firstName;^ ^ Input "Dr Evil" will only set firstName to "Dr":^  ^ Read whole line of text with:string fullName;^ ^ Input "Dr Evil" will set fullName to "Dr Evil".^ ^ It stops on end of line ('\n').

05/06/^

\n detail^ ^ Problem with extra '\n':^ ^ cin >> myString;^ ^ Stops at a '\n', and then...^ ^ getline(cin, myString);^ ^ Stops on first '\n' it finds, even one left from cin!^ ^ Example:^ ^ string lastName, fullName;cout << "Last name: ";cin >> lastName;cout << "Full name: ";getline(cin, fullName);

Input "Evil",press ENTER.E v^ i^ l^ \n lastName gets "Evil",'\n' left in cin buffer.Reads in the extra '\n',sets fullName to 17

Fruit Eating: cin vs getline^ ^ Recap:^ ^ cin:^ ^ skips any leading whitespace & reads data;^ ^ stops on a space or a newline and...^ ^ getline stops on just a newline, but...^ ^ Imagine cin and getline are at a buffet.^ ^ Represent each key-presesd with a fruit:^ (Carrot)^

(Watermelon)^ (Nectarine)

05/06/^

Fruit Eating: cin vs getline^ string lastName, fullName;cout << "Last name: ";cin >> lastName;cout << "Full name: ";getline(cin, fullName);

Last name:^ Evil Full name:^ Dr Evil Secret Agent Program:^ Input stream (read by cin):

Desired Operation:

19

Ignore the '\n'^ ^ Use cin.ignore()^ ^ cin.ignore() will wait until cin hasat least 1 character, and then ignore it!^  ^ Example:^ ^ string lastName, fullName;cout << "Last name: ";cin >> lastName;^ cin.ignore();^ cout << "Full name: ";getline(cin, fullName);

Input "Evil",press ENTER.E v^ i^ l^ \n lastName gets "Evil",'\n' left in cin buffer.Input "Dr Evil";sets fullName to"Dr Evil"^ 05/06/

Fruit Eating: cin vs getline – Fixed!^ string lastName, fullName;cout << "Last name: ";cin >> lastName;cin.ignore();cout << "Full name: ";getline(cin, fullName);

Last name:^ Evil Full name:^ Dr Evil Secret Agent Program:^ Input stream (read by cin):

Desired Operation: 22 Hand DebuggingSection 3.

05/06/^

Hand tracing^ ^ Analyzing Code^ ^ Programmers must be able to trace through sourcecode by hand.^ ^ Used to:^ ^ Understand some code.^ ^ Find & fix bugs!^ ^ Hand tracing:^