This should be solved in the wordcount. cpp file. Write a program that prompts t
ID: 3666006 • Letter: T
Question
This should be solved in the wordcount. cpp file. Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a "word" to be any text that is surrounded by whitespace (for example, a space, carriage return, newline, etc.) or borders the beginning or end of the file. The program should print a string of text to the terminal before getting each piece of input from the user. A session should look like one of the following examples (including whitespace and formatting), with a possibly number and filename in the output: Enter filename: words.txt There are 1000 words in the file "words.txt". Enter filename: oneword.txt There is 1 word in the file "oneword.txt". The string printed by the program should include a newline at the end, but no other trailing whitespace (whitespace at the end of the line).Explanation / Answer
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "Enter filename: ";
string fileName;
getline(cin,fileName);
ifstream inFile; //Declares a file stream object
string word;
int count = 0;
inFile.open(fileName.c_str());
while(!inFile.eof())
{
inFile >> word;
count++;
}
cout << "There are "<<count<<" words in the file ""<<fileName<<"""<<endl;
inFile.close();
return 0;
}
Related 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.