// ************************************************************* // vwlcount.cpp - Vowel Counting Program // // This is a sample program demonstrating file input and output. // It also previews the use of strings and boolean variables. // // It counts the lines, characters and vowels in a text file. // #include // For File I/O #include #include int main() { char current; // current character from file int vwlcount = 0; // vowel count int charcount = 0; // character count int linecount = 0; // line count bool file_output; // Output to file? (otherwise screen) char input_name[80]; // name of input file char output_name[] = "VWLSTAT.TXT"; // name of output file // Declare File Variables ifstream INFILE; ofstream OUTFILE; // Prompt User cout << "Vowel Counter" << endl << endl; cout << "Enter name of file to examine: "; cin >> input_name; cout << "Output to a file (y/n)? "; cin >> current; if (current == 'Y' || current == 'y') { file_output = true; cout << "Output will be sent to: " << output_name << endl; } else { file_output = false; cout << "Output will be sent to the screen" << endl; } // Open Files and check Status INFILE.open(input_name); if (INFILE.bad() ) { cout << "Error Opening INPUT file" << endl; return 1; } // Priming Read INFILE.get(current); // Process Input File while ( !INFILE.eof() ) { switch (current) { case 'A': case 'E': case 'I': case 'a': case 'e': case 'i': case 'o': case 'u': case 'O': case 'U': vwlcount++; break; case '\n': linecount++; break; default: charcount++; break; } // end switch // TRY Both of these statements (one at a time) to see the difference // INFILE.get(current); // INFILE >> current; }// end while loop cout << "Done processing " << input_name << endl; // Output if (file_output) { OUTFILE.open(output_name); if (OUTFILE.bad() ) { cout << "Error Opening OUTPUT file" << endl; return 1; } OUTFILE << "This file contained: " << endl << endl; OUTFILE << "\t" << setw(5) ; OUTFILE << linecount << " lines" << endl; OUTFILE << "\t" << setw(5) ; OUTFILE << charcount << " characters" << endl; OUTFILE << "\t" << setw(5) ; OUTFILE << vwlcount << " vowels" << endl; OUTFILE.close(); } // end output to file else { cout << "This file contained: " << endl << endl; cout << "\t" << setw(5) ; cout << linecount << " lines" << endl; cout << "\t" << setw(5) ; cout << charcount << " characters" << endl; cout << "\t" << setw(5) ; cout << vwlcount << " vowels" << endl; } // end output to file // Close Input File INFILE.close(); return 0; }// end main