cout Characters iNbrOfChars endl Lines iNbrOfLines endl Extracts all characters

Cout characters inbrofchars endl lines inbroflines

This preview shows page 58 - 64 out of 64 pages.

cout << "Characters: " << iNbrOfChars << endl << "Lines: " << iNbrOfLines << endl; Extracts all characters Counting All Characters Characters
Image of page 58
#include <iostream> #include <fstream> using namespace std; int main() { ifstream fin("mydata.txt"); int iValuesProcessed = 0; float fValueSum = 0; float fValue; while ( fin >> fValue ) { fValueSum += fValue; ++iValuesProcessed; } if (iValuesProcessed > 0) { ofstream fout("average.txt"); float fAverage = fValueSum / iValuesProcessed; fout << "Average: " << fAverage << endl; return 0 ; } else { cerr << "No list to average" << endl; return 1 ; } } File Processing File Processing
Image of page 59
The The Do Do - - While While Statement Statement do { Action ; } while ( Expression ) Syntax Action true false Expression Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces
Image of page 60
Waiting for a Proper Reply char chReply; do { cout << "Decision (y, n): "; if (cin >> chReply) chReply = tolower(chReply); else chReply = 'n'; } while ((chReply != 'y') && (chReply != 'n'));
Image of page 61
Iteration Do Iteration Do s s Key Points Make sure there is a statement that will eventually terminate the iteration criterion The loop must stop! Make sure that initialization of loop counters or iterators is properly performed Have a clear purpose for the loop Document the purpose of the loop Document how the body of the loop advances the purpose of the loop
Image of page 62
Home works Home works Exercises 4.1 ~ 4.3 Exercises 4.12~4.14 Exercises 4.16, 4.17 Exercises 4.37, 4.42, 4.47
Image of page 63
End of Chapter 4 End of Chapter 4
Image of page 64

You've reached the end of your free preview.

Want to read all 64 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes