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?

Samran, you are correct in your examples.

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.

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

THanks i figured it out! thank you soo much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.