Intro to Computing Concepts - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

Intro to Computing Concepts

Description:

controls the number of significant digits (total number of ... You entered Eure. 13. Review. Stream Manipulators. setw. setprecision. fixed. showpoint. 14 ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 34
Provided by: markfo7
Category:

less

Transcript and Presenter's Notes

Title: Intro to Computing Concepts


1
Intro to Computing Concepts
  • Lecture 6
  • Mark Fontenot

2
Objectives
  • New Stream Manipulators
  • Advanced Math Functions

3
  • Stream Manipulators

4
Stream Manipulators
Quick Review
What is one stream manipulator weve seen so far?
cout ltlt Hello ltlt endl
5
setw
  • 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) _
6
setprecision
  • 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

7
fixed
  • 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.
8
setprecision 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
9
setprecision fixed
  • Figures
  • ------------
  • 1.23
  • 1.23
  • 3.10

Notice the 1st and 2nd numbers were rounded.
10
showpoint
  • 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_
11
setw 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

12
setw 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
13
Review
  • Stream Manipulators
  • setw
  • setprecision
  • fixed
  • showpoint

14
Other uses of cin
15
getline
Allows the programmer to accept strings
with spaces in them
char string20 cin.getline(string, 19, \n)
16
cin.getline
  • Allows you to read a complete line of input
  • Reads characters up to when user presses ltentergt
  • Will accept spaces and tabs also

17
cin.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!
18
cin.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 _
19
cin.get
int main() char myChar coutltltPress Enter
cin.get(myChar) coutltltThank youltltendl
return 0
keyboard buffer
\n
Press Enter Enter
Thank you _
20
cin.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
21
cin.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 _
22
cin.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

23
cin.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
24
cin.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
25
cin.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
  • Math Functions

27
include ltcmathgt
  • abs
  • y abs(x)
  • Returns the absolute value of the argument.
  • The argument and the return value are integers

28
include 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

29
include ltcmathgt
  • log
  • y log(x)
  • Returns the natural logarithm of the argument
  • The argument and the return value are doubles

30
include ltcmathgt
  • sqrt
  • y sqrt(x)
  • Returns the square root of the argument.
  • The argument and the return value are doubles

31
include 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

32
Exercise
Write a program to ask the user for a and b and
calculate c based on the following formula.
33
Questions?
  • ?
Write a Comment
User Comments (0)
About PowerShow.com