Title: Intro to Computing Concepts
1Intro to Computing Concepts
2Objectives
- New Stream Manipulators
- Advanced Math Functions
3 4Stream Manipulators
Quick Review
What is one stream manipulator weve seen so far?
cout ltlt Hello ltlt endl
5setw
- setw(x)
- print fields of a specified width
int main() int x 28 cout ltlt ( ltlt
setw(5) ltlt x ltlt )ltltendl return 0
( 28) _
6setprecision
- setprecision(x)
- controls the number of significant digits (total
number of digits before and after decimal point
when used alone) - Remains in effect until explicitly changed
7fixed
- fixed
- Forces cout to print digits in fixed-point
notation (versus scientific notation) - Remains in effect until explicitly change
Note When fixed and setprecision are used
together, argument of setprecision is the total
of digits to appear after the decimal point.
8setprecision fixed
- include ltiostreamgt
- include ltiomanipgt
- using namespace std
- int main()
-
- float a 1.23456,
- b 1.226,
- c 3.1
- cout ltlt setprecision(2) ltlt fixed
- cout ltlt Figuresltlt\n------------\n
- cout ltlt setw(8) ltlt a ltlt endl
- cout ltlt setw(8) ltlt b ltlt endl
- cout ltlt setw(8) ltlt c ltlt endl
- return 0
New header file
9setprecision fixed
- Figures
- ------------
- 1.23
- 1.23
- 3.10
Notice the 1st and 2nd numbers were rounded.
10showpoint
- showpoint forces cout to include trailing zeros
when printing
float x 123.4, y 456.0 cout ltlt
setprecision(6) ltlt showpoint ltlt x ltlt endl ltlt
y
123.400456.000_
11setw with cin
- Only works with strings
- Can NOT use with any number-based type (int,
float, etc). cin will take all numbers. - Tell the program how many characters to extract
from the input stream
12setw with cin
- includeltiostreamgt
- includeltiomanipgt
- using namespace std
- int main()
-
- char word5
- cout ltlt Enter a word
- cin gtgt setw(5) gtgt word
- cout ltlt You entered
- ltlt word ltlt endl
- return 0
Enter a word Eureka
You entered Eure _
word
\0
E
u
r
e
13Review
- Stream Manipulators
- setw
- setprecision
- fixed
- showpoint
14Other uses of cin
15getline
Allows the programmer to accept strings
with spaces in them
char string20 cin.getline(string, 19, \n)
16cin.getline
- Allows you to read a complete line of input
- Reads characters up to when user presses ltentergt
- Will accept spaces and tabs also
17cin.getline
- include ltiostreamgt
- include ltiomanipgt
- int main()
-
- char string15
- cout ltlt Enter a sentence
- cin.getline(string, 15)
- coutltltYou entered ltlt string ltltendl
- return 0
string
Enter a sentence See spot run!
You entered See spot run!
18cin.get
- cin.get(ch) retrieves only one character from the
buffer
int main() char myChar coutltltEnter a
char cin.get(myChar) coutltltYou entered
ltltmyCharltltendl return 0
Enter a char x
You entered x _
19cin.get
int main() char myChar coutltltPress Enter
cin.get(myChar) coutltltThank youltltendl
return 0
keyboard buffer
\n
Press Enter Enter
Thank you _
20cin.get and cin gtgt
int main() char myChar int x
coutltltEnter a number cin gtgt x
coutltltEnter a character ltltendl
cin.get(myChar) coutltltThank you!ltltendl
return 0
x
keyboard buffer
159
159\n
Enter a number 159Enter
21cin.get and cin gtgt
int main() char myChar int x
coutltltEnter a number cin gtgt x
coutltltEnter a character ltltendl
cin.get(myChar) coutltltThank you!ltltendl
return 0
159
x
keyboard buffer
\n
myChar
\n
Enter a number 159Enter Enter a character
Thank you _
22cin.ignore()
- cin.ignore(n, c)
- Used to ignore a certain number of characters
- n is the number of characters to ignore
- when cin.ignore(n,c) encounters character c, it
will stop ignoring characters - cin.ignore()
- will only ignore the next character
23cin.get and cin gtgt
int main() char myChar int x
coutltltEnter a number cin gtgt x
cin.ignore() coutltltEnter a character
ltltendl cin.get(myChar) coutltltThank
you!ltltendl return 0
159
x
keyboard buffer
myChar
\n
Enter a number 159Enter Enter a character
24cin.get and cin gtgt
int main() char myChar int x
coutltltEnter a number cin gtgt x
cin.ignore() coutltltEnter a character
ltltendl cin.get(myChar) coutltltThank
you!ltltendl return 0
159
x
keyboard buffer
m
myChar
m\n
Enter a number 159Enter Enter a character
mEnter
25cin.get and cin gtgt
int main() char myChar int x
coutltltEnter a number cin gtgt x
cin.ignore() coutltltEnter a character
ltltendl cin.get(myChar) coutltltThank
you!ltltendl return 0
159
x
keyboard buffer
m
myChar
\n
Enter a number 159Enter Enter a character
mEnter
Thank you _
26 27include ltcmathgt
- abs
- y abs(x)
- Returns the absolute value of the argument.
- The argument and the return value are integers
28include ltcmathgt
- cos
- y cos(x)
- Returns the cosine of the argument
- The arugument should be an angle expressed in
radians. - The argument and the return value are doubles
29include ltcmathgt
- log
- y log(x)
- Returns the natural logarithm of the argument
- The argument and the return value are doubles
30include ltcmathgt
- sqrt
- y sqrt(x)
- Returns the square root of the argument.
- The argument and the return value are doubles
31include ltcmathgt
- pow
- y pow(x,y)
- Returns the value of x raised to the power of y.
- The argument and the return value are integers
32Exercise
Write a program to ask the user for a and b and
calculate c based on the following formula.
33Questions?