Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This should be solved in the averwordlen.cpp file. Write a program that prompts

ID: 3696902 • Letter: T

Question

This should be solved in the averwordlen.cpp file. Write a program that prompts the user to input the name of a text file and then outputs the average word length (average number of characters per word) in the file. Each word is always separated by whitespaces (including newlines). There may be a single comma or period which would appear only at the end of each word, and they are not counted as part of the word. Hence, the following example text is not part of our test cases: Com,mas or per.iods between words do not exist. Other symbols, such as apostrophes in the abbreviated word, contributes to the length of the word. For example, the word "isn't" has the length of 5. Also consider the following special cases. If there is no word in the file, prompt warning "No word in the file!". If the filename entered does not exist, prompt warning "No such file exists." and immediately terminate the program. Our test cases will always end with a file name of a nonexistent file; use this as a mechanism to terminate your program. The program should print a string of text to the terminal before getting each piece of input from the user. Suppose that in a session, the file "word1.txt" contains the following content: The execution of the program might look like the following (including whitespace and formatting), with possibly different numbers and file names in the output: The string printed by the program should include a newline at the end, but no other trailing whitespace. The average word length must be displayed to exactly two digits after the decimal point.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
cout<<"Enter the name of the file:";
string name,temp;
cin>>name;
ifstream in;
in.open(name);
if(!in.is_open()){cout<<"The file does not exist"<<endl;return 0;}
double sum=0;
int cnt=0,len;
while(in>>temp)
{
len = temp.length();
if(temp.at(len-1)=='.' || temp.at(len-1)==',')
len--;
sum+=len;
//cout<<temp<<len<<endl;
cnt++;
}
cout<<"The mean is "<<setprecision(3)<<sum/cnt<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote