Question: Using Files - String or word processing Based on a problem from Nell Dale, Chip Weems and Mark Headington, Programming and Problem Solving with C++

Using Files - String or "word" processing Based 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.

I tried to do

#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 << "Enter the name of file " << "(enter quit to exit): "; cin >> 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 cout << "Number of words in file: " << count << endl; // Calls needed to reuse ifstream variable inFile.close(); inFile.clear(); // Again? cout << "Enter the name of the file " << "(type quit to exit): "; cin >> fileName; } return 0; }

and named all files in input1, input2, and so on

but the result comes out as

Enter the name of file (enter quit to exit): input1

Assertion failed: (inFile), function main, file /Users/Bosco/Desktop/school/spring 18/A5.3/A5.3/a5_3.cpp, line 34.

(lldb)

I don't know what is wrong can anyone help

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!