Question
Please fix my code. The output is not the same as above. Code must satisfy what is in the comments. #include #include #include using namespace
Please fix my code. The output is not the same as above. Code must satisfy what is in the comments.
#include
#include
#include
using namespace std;
int main() {
string filename;
cout
cin >> filename;
ifstream infile;
infile.open(filename);
int lines = 0;
int words = 0;
string word;
string line;
long char_count;
if(infile.fail()) {
cout
return 1;}
infile.seekg(0, ios:: end);
char_count = infile.tellg();
cout
while(getline(infile, line)) {
++lines;}
cout
while (infile >> word) if (word != "") {
words+= 1;}
cout
infile.close();
return 0;
}
. Comment 2: Empty lines are lines. See the sample output below. . Comment 3: For counting characters use the tellg () that returns the position number of get pointer. This can be done as follows: fs. Seekg(0, 10s: :end); long char-count = fs.telig(); Do not count characters directly! . Comment 4: Any text between two spaces is a word. Turn a string into stringstream and use the operator >> to count words. An empty word is not a word! Sample input-output: test.txt - Notepad File Edit Format View Help Hello class Empty 1ines are lines. word collection of characters separated by spaces and newline character . I C:Windowslsystem32\cmd.exe Enter a file name: test.txt Number of characters129 Number oF WOrds Number of lines = 18 Enter a file name: test100.txt File test100.txt does not exist! Exit. Press any key to continue.. . _
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started