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;
}

You don't need using namespace std; in the functions. You already have it at the top. It may not hurt anything, but it's unnecessary and generally is only put at the top of the program.

You have an ifstream in main that you don't really use. You have ifstreams in your functions too. I'd have one in main or have one in your functions, but not both. You can pass the ifstream to your functions from main if you like. You open the file in main, don't close it, then open it again in char_count. You should close it before opening it again. I would get rid of the ifstream in main and keep them in the functions. Open them in the functions, then close them before the function is over. That way you don't have to pass them.

Your problems may well be elsewhere, but I'd make these changes first.

Will you please give me examples as I cannot understand what do you mean by characters.

You want to count all the
1. ALPHABETS (letters)
2. CHARACTERS (like ; ' - + etc)
3. NON WHITE SPACES (includes all file except SPACES).

Am I right?? Do you want to include the numbers (123 etc ) as well?

I hope you'll understand the program (attached).

There are three functions in it. You can even use only one function by sending reference parametes in it, and counting the alphabets, digits and symbols by three variables, one specified for one of these.
The conditions will remain the same, of course, for counting them as they are in separate three functions.

Attachments
//19-march-09		01.51

#include <iostream>
#include <fstream>

using namespace std;

int number_of_letters ();
int number_of_symbols ();
int number_of_digits ();

int main ()
{
	int no_of_letters = 0;
	int no_of_symbols = 0;
	int no_of_nonspaces = 0;
	int no_of_digits = 0;
	ofstream outfile;

	outfile.open ("output16.txt");

	if (outfile.fail ())
	{
		cout << "File not opened!" << endl;

		exit (1);
	}

	no_of_letters = number_of_letters ();

	no_of_digits = number_of_digits ();

	no_of_symbols = number_of_symbols ();

	no_of_nonspaces = no_of_letters + no_of_digits + no_of_symbols;

	outfile << "Number of Letters = " << no_of_letters << endl
			<< "Number of Digits = " << no_of_digits << endl
			<< "Number of Symbols = " << no_of_symbols << endl
			<< "Number of Nonspaces = " << no_of_nonspaces << endl;

	outfile.close ();
	outfile.clear ();
	outfile.flush ();

	cout << "Number of Letters = " << no_of_letters << endl
		 << "Number of Digits = " << no_of_digits << endl
		 << "Number of Symbols = " << no_of_symbols << endl
		 << "Number of Nonspaces = " << no_of_nonspaces << endl;

	return 0;
}

int number_of_letters ()
{
	ifstream infile;
	char temp = '0';
	int count = 0;

	infile.open ("input16.txt");

	if(infile.fail ())
	{
		cout << "File not opened!" << endl;

		exit (1);
	}

	while (!infile.eof ())
	{
		infile.get (temp);

		if (int (temp) >= 65 && int (temp) <= 90 || int (temp) >= 97 && int (temp) <= 122)		//checks the ASCII value of A-Z or a-z
		{
			count++;
		}
	}

	infile.close ();
	infile.clear ();

	return count;
}

int number_of_symbols ()
{
	ifstream infile;
	char temp = '0';
	int count = 0;

	infile.open ("input16.txt");

	if(infile.fail ())
	{
		cout << "File not opened!" << endl;

		exit (1);
	}

	while (!infile.eof ())
	{
		infile.get (temp);

		if (int (temp) >= 33 && int (temp) <= 47 || int (temp) >= 58 && int (temp) <= 64
			|| int (temp) >= 91 && int (temp) <= 96 || int (temp) >= 123 && int (temp) <= 255)	//checks the ASCII value of symbols
		{
			count++;
		}
	}

	infile.close ();
	infile.clear ();

	return count;
}

int number_of_digits ()
{	
	ifstream infile;
	char temp = '0';
	int count = 0;

	infile.open ("input16.txt");

	if(infile.fail ())
	{
		cout << "File not opened!" << endl;

		exit (1);
	}

	while (!infile.eof ())
	{
		infile.get (temp);

		if (int (temp) >= 48 && int (temp) <= 57)		//checks the ASCII value of digits
		{
			count++;
		}
	}

	infile.close ();
	infile.clear ();

	return count;
}

Samran, you left out a function that counted all the characters in the file excluding the white space and '\n's.

This article has been dead for over six months. Start a new discussion instead.