// Example: (really to show some basic loops) v. 3/28/06 // Count the number of X's in a file. (I don't have any // idea why). #include #include #include int main () { char Letter; int LetterCt=0; int XCt=0; double PerCent; fstream InFile; // Keep reading characters counting x's until a z is read. cout << "Enter some characters including at least one z" << endl; XCt = 0; InFile.open( "letters.txt", ios::in ); while ( InFile >> Letter ) { if ( Letter == 'z' ) break; if ( Letter == 'x' ) XCt++; LetterCt++; } cout << LetterCt << " Letters read." << endl; cout << XCt << " x's found." << endl; // Illustrate some mixed mode arithmetic. PerCent = XCt/LetterCt * 100; cout << setprecision( 5 ) << setw( 2 ) << PerCent << " percent x's" << endl; PerCent = (float)XCt/LetterCt * 100; cout << setprecision( 5 ) << setw( 2 ) << PerCent << " percent x's" << endl; PerCent = (XCt*1)/LetterCt * 100; cout << setprecision( 5 ) << setw( 2 ) << PerCent << " percent x's" << endl; PerCent = (XCt*1.0)/LetterCt * 100; cout << setprecision( 5 ) << setw( 2 ) << PerCent << " percent x's" << endl; return 0; }