completing calcs within program

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char 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;
}
I had to make a couple changes and after that it ran great! Nice work. Here are my changes.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
Thank you appreciate it
Topic archived. No new replies allowed.