Using Files - String or \"word\" processing Based on a problem from Nell Dale, C
ID: 3652003 • Letter: U
Question
Using Files - String or "word" processingBased on a problem from Nell Dale, Chip Weems and Mark Headington, Programming and Problem Solving with C++
Write a program that prints out the number of words in a file of text. We will define a word to be any sequence of non-whitespace characters. So "hi&there...mom" would be considered a single word. Solve this problem by using a string variable into which you input each word as a string. Here are some hints for assignment 5.3. Your program should ask the user for the name of the file to count words in (see the Chapter #13 section 13.1 in the Gaddis text titled "Using Variable File Names" on page 831). It should loop until the user types "quit" for the name of the file.
Turn in your source code, followed by an output which has the user entering these 5 input files: file 1 | file 2 | file 3 | file 4 | file 5
File 1:
This &%file should!!,...
have exactly 7 words.
File 2:
This is a &%file that should!!,...
have exactly 10 words.
File 3:
This is a &%file that should!!,...
This file must have several spaces at the end of the file.
have 21 words.
File 4:
This &%file that has!!,...
'
This file must have several spaces at the end of the file.
19 words.
File 5:
Mr. &%Brown can moo!!,...
'
can you? This file should have several blank lines before the end of the file.
22 words.
Using notepad......I suggest that you copy/paste the text from each of these five web pages to create five separate text files. Save these files in the same folder in which you have your working copy of your source code.
File and Data validation: Appropriate validation methods should be used to ensure that the file stream is not in fail state. In other words you will need to include validation routines to check the following (1) whether the data file opened successfully and (2) the data read from the file matches the variable (datatype) into which the value will be stored. Failure to do so will mean that your program may run but will not produce correct output. Here are some source code hints that you may find helpful in implementing the validation routines.
Turn in your source code followed by 5 program test outputs, using each of the five sample data files given above.
Explanation / Answer
#include #include #include // assert using namespace std; int main() { int count; string fileName; // Input file name string astring; ifstream inFile; // Input file stream // Prompt cout fileName; while (fileName != "quit") { // Open file inFile.open(fileName.c_str()); // Error opening file? assert(inFile); // Counting the words count = 0; while (inFile >> astring) ++count; // Output coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.