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