#include <iostream> #include <fstream> #include <cstdlib> # include <string> usi
ID: 3536814 • Letter: #
Question
#include <iostream>
#include <fstream>
#include <cstdlib>
# include <string>
using namespace std;
int main()
{
int letters = 0;
int spaces = 0;
int punctuation = 0;
int numbers =0;
ifstream fin;
ofstream fout;
string filename;
string outfilename;
cout << "Please enter the name of the file you wish to open: ";
cin >> filename;
fin.open(filename.c_str());
if (fin.fail())
{
cout << "The file can not be opened. ";
system("pause");
exit(1);
}
cout << "Please enter the name of the file you wish to write to: ";
cin >> outfilename;
fout.open(outfilename.c_str());
if (fout.fail())
{
cout<<"Output file opening failed";
system("pause");
exit(1);
}
if(fin.is_open() && fout.is_open())
while(!fin.eof())
{
char c = fin.get();
if (isalpha(c))
{
letters++;
}
if (ispunct(c))
{
punctuation++;
}
if (isspace(c))
{
spaces++;
}
if (isdigit(c))
{
numbers++;
}
}
int total=punctuation+letters+spaces+numbers;
int nonblank=total-spaces;
cout << "File statistics:" <<endl;
cout << "The total number of Characters in the file: "<<total<<endl;
cout << "The total number of Letters in the file: " << letters <<" ";
cout << "The total number of Non-whitespace characters in the file: " << nonblank <<" ";
return 0;
}
Explanation / Answer
instead of last 4 lines before return write this
fout << "File statistics:" <<endl;
fout << "The total number of Characters in the file: "<<total<<endl;
fout << "The total number of Letters in the file: " << letters <<" ";
fout << "The total number of Non-whitespace characters in the file: " << nonblank <<" ";
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.