I have a .cpp file that gathers the number of occurances of a letter in a .txt file and also gathered the count of total letters to get the percentage. I am having difficulty getting the calc to work, when I place it within the output it gives me zeros and when I try to hard code the number to divide by it gives zeros. The number of character is <totocc>; I have been working on this all week and have spun myself in a circle and need a little help straightening out again. Please advise...
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
constchar FileName[] = "D:\\School\\CIS328\\Week 2\\Of_studies.txt";
int totocc = 2169;
int main () {
string lineBuffer;
ifstream inMyStream (FileName); //open my file stream
if (inMyStream.is_open()) {
//create an array to hold the letter counts
int upperCaseCount[26] = {0};
//read the text file
while (!inMyStream.eof() ){
//get a line of text
getline (inMyStream, lineBuffer);
//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')
{
upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
}
}
}
inMyStream.close(); //close the file stream
//display the counts
for (int i= 0; i < 26; i++)
cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;
cout << "Total occurances is: " << totocc << endl;
}
else cout << "File Error: Open Failed";
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main () {
//------------------------------------------------------------------I used a string to hold the file path
string filename;
filename="ReadMe.txt";//------------------------------------change your file path here
ifstream readfile;//---------------------------------------------used to open files
int totocc = 2169;
string lineBuffer;
//ifstream inMyStream (); //open my file stream
readfile.open(filename.c_str());//----------------------------convert to c_string then opens file
if (readfile.is_open()) {//-------------------------------changed all the spots you used your ifstream
//create an array to hold the letter counts
int upperCaseCount[26] = {0};
//read the text file
while (!readfile.eof() ){
//get a line of text
getline (readfile, lineBuffer);
//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')
{
upperCaseCount[int(oneLetter)- 65]++; //make the index match the count array
}
}
}
readfile.close(); //close the file stream
//display the counts
for (int i= 0; i < 26; i++)
cout << char(i + 65) << "\t\t" << upperCaseCount[i] << endl;
cout << "Total occurances is: " << totocc << endl;
}
else cout << "File Error: Open Failed";
system("pause");
return 0;
}