Hey, guys! I need little help for finished my code. I have created the code for analize my text file. I have text file with 2 lines. The program counts the lines of my txt file, length of each line, upper and lower case letters. Code works perfect without errors.
The problem is I've confused creating the functions which count:
1. Spaces in my text file
2. Punctuations such as . , "
For spaces I put % symbol in my text file. I think it allows easier to find spaces and count them.
What is my idea? I think, I should find total length of text file or each line of text file( which I have it) and then create true/false function for example: if ( lenght != letters) then space counter increament. Or another way: if I have total length of text, call true/ false function that if lenght not included Upper and Lower letters and than counter increament.
I don't know, but I still playing with code without result. If you have any idea or can help me to find decision, I'll appreciate!
I put some questions in my code. Thank you!

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const char FileName[] = "c:\\myFile.txt";

int main (){
     
     ifstream inMyStream (FileName);   
     string lineBuffer;
  
     if (inMyStream.is_open()){
           //create an array to hold the letter counts
           int upperCaseCount[26] = {0}; //counter of Up letters
           int lowerCaseCount[26] = {0}; //counter of Low letters
           int lineCount   = 1;          //counter of text lines in the file
           int wordCount   = 1;          //counter of spaces 
           int length = 0;               //counter of length
           int letters[128]  = {0};      //counter of total letters in the file
           int punMarks = 1;             //counter of punctuations(,.")
           while (!inMyStream.eof()){

                 //get a line of text
                 getline (inMyStream, lineBuffer);
                 int count = 1;                  
                 count = lineCount;
                 lineCount ++;                                    
                 cout << "\nText File line #:     " << count << endl;

                 length = lineBuffer.length();                       
                 cout << "Length of the line : " << length << endl;
                 
                 //read through each letter in the lineBuffer
                 char oneLetter;
                 for( int n = 0; n < lineBuffer.length(); ++n ){
                      oneLetter = char( lineBuffer[n] );         //get a letter
                      if (oneLetter >= 'A' && oneLetter <='Z'){ //decide if it is a capital letter
                          upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
                      }
                    
                      if (oneLetter >= 'a' && oneLetter <='z'){ //decide if it is a lower case letter
                          lowerCaseCount[int(oneLetter)- 97]++; //make the index match the count array
                      }
                      
                 }//end for
                 //Question: I have created this function to count total characters in the buffer
                 //I think, I can connect it to my idea for create for after while. 
                 char totLetters;
                 for ( int i = 0; i < lineBuffer.length(); ++i ){
                     totLetters = char( lineBuffer[i] );
                     if ( totLetters == lineBuffer.length()){
                        letters[int(totLetters) - 128]++;
                     }
                 }
            }//end while

            inMyStream.close();

            cout << "\nMy file containts the letters: " << endl;
            cout << "\nUpperCase |  Quantity: " << endl;
            for (int i = 0; i < 26; i++ )
                if( upperCaseCount[i] > 0)
                   cout << "   " << char(i + 65) << "\t  | \t" << upperCaseCount[i] << endl;
 
            cout << "\nLowerCase |  Quantity: " << endl;
            for (int i = 0; i < 26; i++ )
                if(lowerCaseCount[i] > 0)
                  cout << "   " << char(i + 97) << "\t  | \t" << lowerCaseCount[i] << endl;

            // I just leave this functions for review
            // This function should print spaces
            cout << "Text file has spaces: " << wordCount << endl;
            // Should print punctuations and use punMarks counter                
            cout << "Text file has punctuations: " << "," << " and "<< "." << endl;
            cout << endl;
        }
       else
      
          cout << "File Error: Open Failed";
      
     return 0;
}

Recommended Answers

All 3 Replies

This should be easy.

int cntr[256] = {0}: //counter for each character

//read in char.
//increment cntr for whatever you have read in
while(iFile.get( c ) ) {  c = ++c % 256; cntr[c] += 1; }

There are still some caution to take, but this should be sufficient.

Thanks for option! I'm playing with your code, but I have several errors.
I put counter: int cntr[256] = {0}; in main, and
while(MyFile.get( c ) ) { c = ++c % 256; cntr[c] += 1; } after first while. May be I did mistake, but still in Ask about help!
And, one more question, why you put 256 in array, I think it shoul be 128, isn't it?
If you can , make little bit clear! Thank you!

Thanks for option! I'm playing with your code, but I have several errors.
I put counter: int cntr[256] = {0}; in main, and
while(MyFile.get( c ) ) { c = ++c % 256; cntr[c] += 1; } after first while. May be I did mistake, but still in Ask about help!
And, one more question, why you put 256 in array, I think it shoul be 128, isn't it?
If you can , make little bit clear! Thank you!

Its 256 because there are 256 different (narrow) characters, then it
repeats again.

your code should look something like this :

//include libraries 
//using declarative 

int main()
{
   std::ifstream iFile("someFile.txt");
   char c;
   int cntr[256] = {0};
   while( iFile.get(c))
   {
         cout<<c;

             //this is called a circular loop. making sure c is 
             //not above 256 otherwise the array would go 
             //out of bounds.

          c = ++c % 256; 

         cntr[c] +=1;
    }

  return 0;
}
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.