Need Help w/ I/O and Predefined Character Functions

This is a discussion on Need Help w/ I/O and Predefined Character Functions within the C++ Programming forums, part of the General Programming Boards category; Okay, so I am trying to read an input of a file, and output and display it's stats. The stats ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    14

    Question Need Help Finding # Of Characters / Letters / Non-whitespace Characters (Code Inside)

    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;
    }
    Last edited by impact; 05-22-2007 at 02:10 AM. Reason: New Info

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    14
    Nevermind, I finally got everything working correctly. I merged the void functions and did some other changes, now everything works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 10:29 AM
  2. Predefined FileOpen, SaveAs, etc. in C?
    By xjcass in forum Windows Programming
    Replies: 2
    Last Post: 11-08-2003, 09:38 AM
  3. return character arrays
    By Azmeos in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2003, 08:15 AM
  4. Functions problems!
    By Paninaro in forum C Programming
    Replies: 2
    Last Post: 06-18-2002, 07:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21