2007 03 09
Download
Skip this Video
Download Presentation
計算機概論實習 2007-03-09

Loading in 2 Seconds...

play fullscreen
1 / 27

計算機概論實習 2007-03-09 - PowerPoint PPT Presentation


  • 118 Views
  • Uploaded on

計算機概論實習 2007-03-09. Stream. Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer, etc.). in c scanf("\%c", &ch); in c++ cin >> ch;. byte stream. byte stream. in c printf("\%c", ch); in c++

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '計算機概論實習 2007-03-09' - shelley-hardin


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
stream
Stream
  • Stream: sequence of bytes
    • Input: from device (keyboard, disk drive) to memory
    • Output: from memory to device (monitor, printer, etc.)

in c

scanf("%c", &ch);

in c++

cin >> ch;

byte stream

byte stream

in c

printf("%c", ch);

in c++

cout << ch;

Computer

iostream library header files
iostream Library Header Files
  • iostream library
    • Has header files with hundreds of I/O capabilities
      • Standard input (cin)
      • Standard output (cout)
      • Unbuffered error (cerr)
      • Buffered error (clog)
      • Formatted I/O with parameterized stream manipulators
      • File processing operations
standard input cin
Standard input (cin)
  • Format input
    • cin >> grade
    • Need to include iostream and std:cin
  • Using >> operator
    • Normally skips whitespace (blanks, tabs, newlines)
      • Can change this
    • Returns 0 when EOF encountered
      • Otherwise, returns reference to object
    • State bits set if errors occur
      • Discussed in 12.7 and 12.8
standard output cout
Standard output (cout)
  • Format input
    • cout << grade
    • Need to include iostream and std:cout
  • Using << operator
exmaple code
Exmaple Code

#include

usingnamespace std;

void main(){

int i;

cin >> i;

cout << i;

}

multi input output
Multi-Input/Output

int main(){

int a, b;

cout << “Input two number: ”;

cin >> a >> b;

cout << “The inputs are ” << a << “ and ” << b;

return 0;

}

Input two number: 4 7

The inputs are 4 and 7

space

member function cin get
Member Function: cin.get()
  • get(charRef)
    • With character reference argument
    • Gets only one character, stores in charRef
      • Returns reference to istream
      • If EOF, returns -1
  • get(charArray, size)
  • get(charArray, size, delimiter)
    • Reads until size-1 characters read, or delimiter encountered
      • Default delimiter '\n'
      • Delimiter stays in input stream
        • Can remove with cin.get() or cin.ignore()
    • Makes array null-terminated
member function cin getline
Member Function: cin.getline()
  • getline(array, size)
  • getline(array, size, delimiter)
    • Reads size-1 characters, or until delimiter found
      • Default delimiter '\n'
    • Removes delimiter from input stream
    • Puts null character at end of array
what difference among cin cin get and cin getline 1 3
skip due to >>What Difference Among cin, cin.get(), and cin.getline() (1/3)

int i;

cin >> i;

cout << “i = ” << i;

i = 456

i = 456

what difference among cin cin get and cin getline 2 3
Delimiter stays in input streamWhat Difference Among cin, cin.get(), and cin.getline() (2/3)

char ch[10];

cin.get(ch, 10);

cout << ch;

ch[0] = ‘4’

ch[1] = ‘5’

ch[2] = ‘6’

ch[3] = 0

ch[0] = 0

Nothing is read

what difference among cin get and getline 3 3
Removes delimiter from

input stream

What Difference Among cin, get(), and getline() (3/3)

char ch[10];

cin.getline(ch, 10);

cout << ch;

ch[0] = ‘4’

ch[1] = ‘5’

ch[2] = ‘6’

ch[3] = 0

ch[0] = 0

throw away delimiter
Throw Away Delimiter
  • If use cin.get(), the delimiter will stay in the stream. Using the following method to Throw it Away.

int main() {

char buf[100];

char trash;

while(cin.get(buf,100)) { // Get chars until ‘\n’

cin.get(trash); // Throw away the terminator

cout << buf <<'\n'; // Add the ‘\n’ at the end

} return 0;

}

Input : This is a test

Output: This is a test

stream error states
Stream Error States
  • Test state of stream using bits
    • eofbit set when EOF encountered
      • Function eof returns true if eofbit set
      • cin.eof()
    • failbit set when error occurs in stream
      • Data not lost, error recoverable
      • Function fail()returns true if set
stream error states1
Stream Error States
  • badbit set when data lost
    • Usually nonrecoverable
    • Function bad()
  • goodbit set when badbit, failbit and eofbit off
    • Function good()
stream error states2
Stream Error States
  • Member functions
    • rdstate()
      • Returns error state of stream
      • Can test for goodbit, badbit, etc.
      • Better to test using good(), bad()
    • clear()
      • Default argument goodbit
      • Sets stream to "good" state, so I/O can continue
      • Can pass other values
        • cin.clear( ios::failbit )
        • Sets failbit
        • Name "clear" seems strange, but correct
slide17
int main(){

int integerValue;

// display results of cin functions

cout << "Before a bad input operation:"

<< "\ncin.rdstate(): " << cin.rdstate()

<< "\n cin.eof(): " << cin.eof()

<< "\n cin.fail(): " << cin.fail()

<< "\n cin.bad(): " << cin.bad()

<< "\n cin.good(): " << cin.good()

<< "\n\nExpects an integer, but enter a character: ";

cin >> integerValue; // enter character value

cout << endl;

slide18
// display results of cin functions after bad input

cout << "After a bad input operation:"

<< "\ncin.rdstate(): " << cin.rdstate()

<< "\n cin.eof(): " << cin.eof()

<< "\n cin.fail(): " << cin.fail()

<< "\n cin.bad(): " << cin.bad()

<< "\n cin.good(): " << cin.good() << endl << endl;

cin.clear(); // clear stream

// display results of cin functions after clearing cin

cout << "After cin.clear()"

<< "\ncin.fail(): " << cin.fail()

<< "\ncin.good(): " << cin.good() << endl;

return 0;

}

slide19
Before a bad input operation:

cin.rdstate(): 0

cin.eof(): 0

cin.fail(): 0

cin.bad(): 0

cin.good(): 1

Expects an integer, but enter a character: c

After a bad input operation:

cin.rdstate(): 2

cin.eof(): 0

cin.fail(): 1

cin.bad(): 0

cin.good(): 0

After cin.clear()

cin.fail(): 0

cin.good(): 1

Different error bit set leads to different value of rdstate()

error input format simple example
Error Input Format – Simple Example

int main () {

int a;

while (cin>>a){

cout<<"The input is:" <

}

cout << "fail?" << cin.fail();

}

123

The input is:123

abc

fail? 1

error input format complex example
Error Input Format – Complex Example

123

The input is:123

ab

fail?1

ab1

fail?1

123b

The input is:123

fail?1

1

2

3

4

explanation of example 1
Explanation of Example - 1
  • If there are not any error input, then program can get value from cin stream.

123

while (cin>>a)

a=123

explanation of example 2
Explanation of Example - 2
  • When the input appears format error, the failbit set. In other words, the return value of cin.fail() is 1.
  • But, the cin stream still keep the input value.
    • For example, when you input ab, it will be kept in the stream.

while (cin>>a)

ab

a = -87361838

cin.fail() = 1

explanation of example 4
Explanation of Example - 4
  • When you input 1ab, a will be 1 but the ab will be kept in the cin stream.
  • When you try to get from cin stream again, the failbit set.

1ab

while (cin>>a)

a=1

cin.fail() = 0

First time

a=-8193787

cin.fail() = 1

Second time

error input format another example
Error Input Format – Another Example
  • Reset cin state and clear the stream. Then to request user to re-input with correct format (ex. Integer).
example program
Need include limitsExample Program

// in order to prevent user from inputting incorrect format, clear stream

void clear2eol(){

cin.ignore(numeric_limits::max(), '\n');

}

int main(){

int a;

while (cout << "Enter a number: " && !(cin >> a)) {

cin.clear(); // reset cin iostate as goodbit

clear2eol(); // clear the stream

cout << "Wrong input. ";

}

clear2eol();

return 0;

}

Note that this code can not check the error such as “23abc”

practice 1 p1
Practice 1 (P1)
  • Try to write a program that request user to input the student’s information including name and score and output the information.
    • Please check that if the input of score is correct. If the input is not correct, then to request the user to re-input until the program gets a correct one.
ad