Hey guys i need help. I need to write a program that computes all of the following statistics for a file and outputs the statistics to both the screen and to another file: the total number of occurrences of characters in the file, the total number of nonwhitespace characters in the file, and the total number of occurrences of letters in the file. So far it doesn't work properly. I outputs the number of chars and i don't know how to output as a file, and the numbers aren't coming out right at all, if you could help me that would be great.
//********************************************************************************************
//
// CharacterStats.cpp
//
// Program that reads text from a text file named "input16.txt"
// and writes out the number of characters in the file, along with the
// number of letters and the number of nonwhitespace characters.
// The output is written both to the console and to a file.
//
//********************************************************************************************
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cctype>
int char_count(); //counts all characters
int non_white_space();//counts everything but not white spaces
int letter_count(); //counts only letters
using namespace std;
int main( )
{
// Declare input stream
ifstream fin;
char val;
int while_count = 1;
int count = 0;
int nws = 0;
int letters = 0;
fin.open("input16.txt");
// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
count = char_count();
nws = non_white_space();
letters = letter_count();
// --------------------------------
// --------- END USER CODE --------
// --------------------------------
fin.close();
cout << "The file contains:\n";
cout << " " << count << " characters\n";
cout << " " << nws << " non-whitespace characters\n";
cout << " " << letters << " letters\n";
system("pause");
}
//counts all characters
int char_count(){
char val;
int count = 0;
ifstream fin;
fin.open("input16.txt");
using namespace std;
fin.get();
while(fin.good())
{
if(!fin.eof())
{
val = fin.get();
++count;
if(val==10)
++count;
}
}
fin.close();
return count;
}
//counts letters and numbers and non white spaces
int non_white_space(){
char val;
int nws = 0;
ifstream fin;
fin.open("input16.txt");
using namespace std;
fin.get();
while(fin.good())
{
if(!fin.eof())
{
val = fin.get();
++nws;
if(val==10)
++nws;
}
}
fin.close();
return nws;
}
//counts letters not white spaces
int letter_count(){
char val;
int letters = 0;
ifstream fin;
fin.open("input16.txt");
using namespace std;
fin.get();
while(fin.good())
{
while((!fin.eof())&&(val != 1)&&(val != 2)&&(val != 3)&&(val != 4)&&(val != 5)
&&(val != 6)&&(val != 7)&&(val != 8)&&(val != 9)&&(val != 0)&&(val != ' ')
&&(val !='\n')){
letters++;
}
}
return letters;
}