/
CS  101 - Additional  Examples CS  101 - Additional  Examples

CS 101 - Additional Examples - PowerPoint Presentation

yoshiko-marsland
yoshiko-marsland . @yoshiko-marsland
Follow
400 views
Uploaded On 2018-02-11

CS 101 - Additional Examples - PPT Presentation

Dr Rehab Duwairi cinget Assume you have the following input stream A 25 You want your program to store A in ch1 Blank in ch2 and 25 in num If you write cin gtgtch1gtgtch2gtgt ID: 630269

cout line cin endl line cout endl cin radius height setprecision setw fixed rate hours ch1 ch2 ignore output input decimal tolerance

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS 101 - Additional Examples" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CS 101 - Additional Examples

Dr. Rehab

DuwairiSlide2

cin.get()

Assume you have the following input stream: A 25

You want your program to store ‘A’ in ch1, Blank in ch2 and 25 in num.

If you write

cin

>>ch1>>ch2>>

num

;

ch1 will get 1, ch2 will get 2 and

num

will get 5.Slide3

>> operator skips white spaces: blank, end of line.

You need get() function.

Get function reads a character at a time.

The correct code would be

cin.get

(ch1);

cin.get

(ch2);

cin

>>

num

;Slide4

cin.ignore(intExp,

chExp

);

Sometimes you may want to exclude or ignore certain parts of the input data. Ignore allows you to do so.

intExp

is an integer expression

chExp

is a character expression

Logic: ignore the next

intExp

characters or until you encounter

chExp

which

evercomes

first.Slide5

examples

cin.ignore

(100, '\n

');

it ignores either the next 100 characters or all

characters until the newline character is found, whichever comes first

.

cin.ignore

(100, 'A');

Ignores the

first 100 characters or all characters until the character 'A' is

found, whichever comes first.Slide6

int a, b;

and

the input:

25 67 89 43 72

12 78 34

Now consider the following statements:

cin

>> a;

cin.ignore

(100, '\n');

cin

>> b;

Output:

a=25

b=12Slide7

Consider the declaration:

char ch1, ch2;

and

the input:

Hello there. My name is Mickey.

Consider

the following statements:

cin

>> ch1;

cin.ignore

(100, '.');

cin

>> ch2

;

Output:

ch1 = ‘H’

c

h2 = ‘M’Slide8

Consider the declaration:

char ch1, ch2;

and the input:

Hello there. My name is Mickey.

Consider the following statements:

cin

>> ch1;

cin.ignore

(5, '.');

cin

>> ch2

;

The output:

ch1 = ‘H’

Ch2 = ‘t’Slide9

Output and formatting output

Fixed

cout

<< fixed

; //enable or

c

in.setf

(

ios

::fixed)

cout.unsetf

(

ios

::fixed

); //disable

S

cientific

cin

>>

scientific; //enable or

cin.setf

(

ios

::scientific

);

cin

>> scientific

; Slide10

//Example: scientific and fixed#include <

iostream

>

using namespace

std

;

int

main()

{

double hours = 35.45;

double rate = 15.00;

double tolerance = 0.01000;

cout

<< "hours = " << hours << ", rate = " <<

rate <<

", pay = " << hours * rate

<< ", tolerance = " << tolerance <<

endl

<<

endl

;

cout

<< scientific;

cout

<< "Scientific notation: " <<

endl

;

cout

<< "hours = " << hours << ", rate = " << rate

<< ", pay = " << hours *

rate <<

", tolerance = " << tolerance <<

endl

<<

endl

;

cout

<< fixed;

cout

<< "Fixed decimal notation: " <<

endl

;

cout

<< "hours = " << hours << ", rate = " <<

rate << ", pay

= " << hours * rate

<< ", tolerance = " << tolerance <<

endl

<<

endl

;

return 0;

}Slide11
Slide12
Slide13

showpoint Manipulator

Suppose

that the decimal part of a decimal number is zero. In this case, when you instruct

the computer

to output the decimal number in a fixed decimal format, the output may not

show the

decimal point and the decimal part.

To

force the output to show the decimal point

and

trailing zeros, you use the manipulator

showpoint

.

cout

<<

showpoint

;Slide14

//Example: setprecision

, fixed,

showpoint

#include <

iostream

> //Line 1

#include <

iomanip

> //Line 2

using namespace

std

; //Line 3

const

double PI = 3.14159265; //Line 4

int

main() //Line 5

{ //Line 6

double radius = 12.67; //Line 7

double height = 12.00; //Line 8

cout

<< fixed <<

showpoint

; //Line 9

cout

<<

setprecision

(2)

<< "Line 10:

setprecision

(2)" <<

endl

; //Line 10

cout

<< "Line 11: radius = " << radius <<

endl

; //Line 11

cout

<< "Line 12: height = " << height <<

endl

; //Line 12

cout

<< "Line 13: volume = "

<< PI * radius * radius * height <<

endl

; //Line 13

cout

<< "Line 14: PI = " << PI <<

endl

<<

endl

; //Line

14Slide15

cout <<

setprecision

(3)

<< "Line 15:

setprecision

(3)" <<

endl

; //Line 15

cout

<< "Line 16: radius = " << radius <<

endl

; //Line 16

cout

<< "Line 17: height = " << height <<

endl

; //Line 17

cout

<< "Line 18: volume = "

<< PI * radius * radius * height <<

endl

; //Line 18

cout

<< "Line 19: PI = " << PI <<

endl

<<

endl

; //Line 19

cout

<<

setprecision

(4)

<< "Line 20:

setprecision

(4)" <<

endl

; //Line 20

cout

<< "Line 21: radius = " << radius <<

endl

; //Line 21

cout

<< "Line 22: height = " << height <<

endl

; //Line

22

cout

<< "Line 23: volume = "

<< PI * radius * radius * height <<

endl

; //Line 23

cout

<< "Line 24: PI = " << PI <<

endl

<<

endl

; //Line 24

cout

<< "Line 25: "

<<

setprecision

(3) << radius << ", "

<<

setprecision

(2) << height << ", "

<<

setprecision

(5) << PI <<

endl

; //Line 25

return 0; //Line 26

} //Line 27Slide16

SAMPLE RUN

Line

10:

setprecision

(2)

Line 11: radius = 12.67

Line 12: height = 12.00

Line 13: volume = 6051.80

Line 14: PI = 3.14

Line 15:

setprecision

(3)

Line 16: radius = 12.670

Line 17: height = 12.000

Line 18: volume = 6051.797

Line 19: PI = 3.142

Line 20:

setprecision

(4)

Line 21: radius = 12.6700

Line 22: height = 12.0000

Line 23: volume = 6051.7969

Line 24: PI = 3.1416

Line 25: 12.670, 12.00, 3.14159Slide17

//Example: setw

#include <

iostream

>

#include <

iomanip

>

using namespace

std

;

int

main

() {

int

x = 19; //Line 1

int

a = 345; //Line 2

double y = 76.384; //Line 3

cout

<< fixed <<

showpoint

; //Line 4

cout

<< "12345678901234567890" <<

endl

; //Line 5

cout

<<

setw

(5) << x <<

endl

; //Line 6

cout

<<

setw

(5) << a <<

setw

(5) << "Hi"

<<

setw

(5) << x <<

endl

<<

endl

; //Line 7

cout

<<

setprecision

(2); //Line 8

cout

<<

setw

(6) << a <<

setw

(6) << y

<<

setw

(6) << x <<

endl

; //Line 9

cout

<<

setw

(6) << x <<

setw

(6) << a

<<

setw

(6) << y <<

endl

<<

endl

; //Line 10

cout

<<

setw

(5) << a << x <<

endl

; //Line 11

cout

<<

setw

(2) << a <<

setw

(4) << x <<

endl

; //Line 12

return 0

; }Slide18
Slide19

left and right Manipulators

ostreamVar

<< left

;

ostreamVar.unsetf

(

ios

::left

);

cout.unsetf

(

ios

::left

);

ostreamVar

<< right

;Slide20

Input/Output and the string Type

#include <string>

string name; //variable declaration

cin

>>

name; //input

statement

you

cannot use the

extraction operator (>>)

to read strings that contain

blanks

To read a string containing blanks, you can use the function

getline

.

getline

(

istreamVar

,

strVar

);Slide21

Consider the following statement:

string

myString

;

If

the input is 29 characters:

bbbbHello

there. How are you?

where b represents a

blank:

getline

(

cin

,

myString

);

the value of

myString

is:

myString

=

“ Hello

there. How are you?"

All 29 characters, including the first four blanks, are stored into

myString

.