Okay, so I am trying to read an input of a file, and output and display it's stats. The stats that I am looking for are.. : the total number of characeters, the total number of letters, and total number of non-whitespace characters (which is just the total number of letters + the total number of whitespace characters).
Here is my code so far. Currently, the total number of letters is outputting correctly, but when i add the whitespace function to the code, the whitespace function outputs zero, but when I take the letters function out of the code, it outputs the correct number? Any reason why this is happening?
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void whiteSpace (ifstream& in_stream, ofstream& out_stream) // Count WhiteSpaces
{
int wS = 0;
char w;
in_stream.get(w);
do
{
if (isspace(w))
wS++;
in_stream.get(w);
} while (w != '.');
cout << "The total count of non-whitespace characters is : " << wS << "." << endl;
out_stream << "The total count of non-whitespace characters is : " << wS << "." << endl;
}
void letters (ifstream& in_stream, ofstream& out_stream) // Count Letters
{
int aC = 0;
char c;
in_stream.get(c);
while (! in_stream.eof())
{
if (isalpha(c))
aC++;
in_stream.get(c);
}
cout << "The total count of letters is : " << aC << "." << endl;
out_stream << "The total count of letters is : " << aC << "." << endl;
}
int main () // Main Function
{
int out_stream;
ifstream fin;
ofstream fout;
fin.open("ex16in.txt");
if (fin.fail())
{
cout << "Failed to open input file!\n";
system("PAUSE");
exit(1);
}
fout.open("ex16out.txt");
if (fout.fail())
{
cout << "Failed to open input file!\n";
system("PAUSE");
exit(1);
}
letters(fin, fout);
whiteSpace(fin, fout);
fin.close(); // Close Input File
fout.close(); // Close Output File
system("PAUSE");
return 0;
}