white spaces
DESCRIPTION
White Spaces. Space Tab Next line …. Input Operator >>. cout > theInput; // Operator >> will ignore white spaces // before reading // Operator >> will stop at white spaces // after have read any value // Input: -50.5 50 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/1.jpg)
1
CS 1430: Programming in C++
![Page 2: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/2.jpg)
2
Find Max, Min, Average of m Sections
Max, Min and Average of each section
Max, Min and Average of all sections together
![Page 3: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/3.jpg)
3
Pseudo Code
While not end of class
Process one section
While not end of class
While not end of section
Process one score
![Page 4: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/4.jpg)
4
Pseudo CodeInitialize class variables (LCV)
While not end of class
Initialize section variables (LCV)
While not end of section
Process one score
Update section LCV
Process and display section result
Process class variables
Update class LCV
Process and display class result
![Page 5: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/5.jpg)
5
Input
50.5 54 39 47.5 -1 50 49.5 52 -1 38 49 -1 50 59.5 53 60 -1 -2
Sentinel-Controlled Loop -1: end of section -2: end of class Range is not known All scores are valid
![Page 6: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/6.jpg)
6
Pseudo Code
classTotal = 0, classCount = 0Input a score (Prime Read)While score not -2 secTotal = 0 secCoumt = 0 While score not -1 Update secCoumt, secTotal, secMax, secMin Input next score Process and display section result Update classMax, classMin, classCount, classTotal Input next scoreProcess and display class result
![Page 7: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/7.jpg)
7
// Constants // Before main()const int END_CLASS = -2;const int END_SECTION = -1; int main(){ // Declare variables // Inside main() float score; float classMax, classMin, classTotal; float secMax, secMin, secTotal; int classScoreCount, secScoreCount;
… return 0;}
![Page 8: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/8.jpg)
8
classTotal = 0.0;classScoreCount = 0;cin >> score; // Prime Read
while (score != END_CLASS){ secTotal = 0; secScoreCount = 0; while (score != END_SECTION) { if (secScoreCount == 0) { secMax = score; secMin = score; } else { if (score > secMax) secMax = score; if (score < secMin) secMin = score; } secScoreCount ++; secTotal += score;
cin >> score; }
// Display section result
if (classScoreCount == 0)
{
classMax = secMax;
classMin = secMin;
}
else
{
if (secMax > classMax)
classMax = secMax;
if (secMin < classMin)
classMin = secMin;
}
classTotal += secTotal;
classScoreCount += secScoreCount;
cin >> score;
}
// Process and display class result
![Page 9: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/9.jpg)
9
Different Input
4 4 50.5 54 39 47.5 3 50 49.5 52 2 38 49 4 50 59.5 53 60
Count-Controlled Loop Range is known Checking range Ignore invalid scores Can you do it?
![Page 10: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/10.jpg)
10
White Spaces
SpaceTab
Next line…
(Replace Tab with 3 spaces in VS)
![Page 11: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/11.jpg)
11
Input Operator >>
cout << "What is your input: ";
cin >> theInput;
// Operator >> will ignore white spaces // before reading// Operator >> will stop at white spaces// after have read any value
// Input: -50.5 50
// What is the value of theInput?
// Depending on the type of theInput
// But the spaces before “-50.0” will be ignored.
![Page 12: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/12.jpg)
12
Type char
char theInput;
cout << "What is your input: ";
cin >> theInput;
// Input: -50.5 50
// Q: What is the value of theInput?
// A: _____
// ‘-’
// Not space before ‘-’
![Page 13: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/13.jpg)
13
Type integer
int theInput;
cout << "What is your input: ";
cin >> theInput;
// Input: -50.5 50
// Q: What is the value of theInput?
// A: _____
// -50
// stop at ‘.’, which cannot be part of int
![Page 14: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/14.jpg)
14
Type float
float theInput;
cout << "What is your input: ";
cin >> theInput;
// Input: -50.5 50
// Q: What is the value of theInput?
// A: _____
// -50.5
![Page 15: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/15.jpg)
15
Type string
string theInput;
cout << "What is your input: ";
cin >> theInput;
// Input: -50.5 50
// Q: What is the value of theInput?
// A: _____
// -50.5
![Page 16: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/16.jpg)
16
// Input// CS 143// X
char aChar;int count = 0;
cin >> aChar; // Prime read before loopwhile (aChar != ‘X’){ count ++; cout << aChar;
cin >> aChar;}cout << aChar;cout << “Count: “ << count;
Tracing
aChar
C
S
1
4
3
X
![Page 17: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/17.jpg)
17
Function cin.get()
The function will read one char at a time
Including white spaces!
![Page 18: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/18.jpg)
18
// Input// CS 143// X
char aChar;int count = 0;
cin.get(aChar);// Prime read before loopwhile (aChar != ‘X’){ count ++; cout << aChar; cin.get(aChar);}cout << aChar;cout << “Count: “ << count;
Tracing
aChar
C
S
-
1
4
3
\n
X
![Page 19: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/19.jpg)
20
Function cin.eof()
The function returns true or falseeof(): end of file
end of input
From keyboard[CTRL-D]
From FileNot True after reading the last item in the file
True when trying to read after reading the last item in the file
![Page 20: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/20.jpg)
21
// Input// CS 143// X
char aChar;int count = 0;
cin.get(aChar);// Prime read before loopwhile (!cin.eof()){ count ++; cout << aChar; cin.get(aChar);}cout << aChar;cout << “Count: “ << count;
Tracing
aChar cin.eof()
C False
S False
- False
1 False
4 False
3 False
\n False
X False
? True
![Page 21: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/21.jpg)
22
int theInput; int count = 0;
cout << "What is your input: "; cin >> theInput;
while (!cin.eof()) { count ++; cout << "What is your input: "; cin >> theInput; }
cout << theInput;cout << “Count: “ << count;
// Input: 55 50.5 60// What is the value of theInput?// Trouble when reading ‘.’!
![Page 22: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/22.jpg)
23
Formatting Output
cout << endl << "My output.";
cout << "\nMy output.";
float average;
int count;
// Compute count and average
cout << endl << count;
cout << endl << average; // How many digits displayed for average?
![Page 23: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/23.jpg)
24
#include <iomanip>
cout << fixed << showpoint << setprecision(2);
// Always display two decimal digits.
// No need to remember for quiz/test
float value;
value = 1 / 3.0;
cout << "\nFloat value 1/3.0: " << value;
// 0.33
value = 36 / 3.0;
cout << "\nFloat value 36/3.0: " << value;
// 12.00
value = 1 / 5.0;
cout << "\nFloat value 1/5.0: " << value;
// 0.20
![Page 24: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/24.jpg)
25
#include <iomanip> cout << fixed << showpoint << setprecision(2); // The setting is applied to all values displayed// until the setting changes.
float value;
value = 1 / 3.0;cout << "\nFloat value 1/3.0: " << setw(8) << value;// □ □ □ □ 0.33
value = 36 / 3.0;cout << "\nFloat value 36/3.0: " << setw(8) << value;// □ □ □ 12.00
value = 1 / 5.0;cout << "\nFloat value 1/5.0: " << value;// 0.20 (no extra spaces)
// setw() is only applied to the next value// It has to be used for every value need the format.
![Page 25: White Spaces](https://reader035.vdocuments.us/reader035/viewer/2022062314/56813352550346895d9a608c/html5/thumbnails/25.jpg)
30
Schedule• Prog1
Can still fix style
• Prog2: Loops
Start Early! Two points Quiz Monday!
• Lab 2
Demo by 4pm
• Quiz3-4
Due 5 pm Monday
• Test 1: Next Friday
Note01 – Note10
Prog2: Loops!
• Quiz3-5
Now!