{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Infilefail coutx nn1 infilex coutreadinnintegersendl

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { cerr << “Problem with file” << endl; stream.fail() can be exit(EXIT_FAILURE); used with any stream } and returns true if infile >> x >> y >> z; the last action failed. Using Selection To Check Using Selection To Check Program Status int x, n=0; ifstream infile(“myfile.data”); infile >> x; while (! infile.fail() ){ cout << x; n = n + 1; infile >> x; } cout << “read in “ << n << “ integers.” << endl; fail() can also be used to read fail() can also be used to read files of arbitrary length Switch Statements Switch Statements A switch statement is used to decide between a number of cases switch (integer_expression){ case value: expression case value: expression default: expression } The integer expression is evaluated and the thread of execution starts from the matching case. If none matches it starts from the default. Switch Statements Switch Statements switch (weekday){ case 1: cout << “Monday”; breaks are break; needed. case 2: cout << “Tuesday”; Otherwise break; switch drops case 3: cout << “Wednesday”; through to break; next expression. case 4: cout << “Thursday”; break; case 5: cout << “Friday”; break; default: cout << “Weekend”; } Execution Control Sequence Selection Iteration Two Important Halves of Two Important Halves of Computer Programming Data Representation How do we structure the data that...
View Full Document

{[ snackBarMessage ]}