Counting Program

Hi, I've been learning basic C++ for a couple months now and I'm currently writing a program to count letters from a .txt file and then output the number of each letter present. At the moment I have managed to get the program to open the file and begin counting, however it counts all the letters as the letter a.

The text file is made up only mixed numbers and letters (but only lower case) so I dont need to worry about converting them.

The program doesnt need to count spaces or the numbers just the letters.

My function is as follows;

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	// start reading characters
	int main() 
{
 	ifstream inFile;
	ofstream outFile;

	inFile.open ("code.txt");
	outFile.open ("result.txt");
	// if cant find file display error
	if (!inFile) 		{
    					cout << "Cant find file input";
					
    					}

	
	// else the program starts
	else {
		inFile >> letter;
   		while (!inFile.eof()) {
   									
  
			if (letter = static_cast<int>(97))
			{a_count ++;
					inFile >> letter;
			}
			else if (letter = static_cast<int>(98))
			{b_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(99))
			{c_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(100))
			{d_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(101))
			{e_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(102))
			{f_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(103))
			{g_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(104))
			{h_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(105))
			{i_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(106))
			{j_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(107))
			{k_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(108))
			{l_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(109))
			{m_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(110))
			{n_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(111))
			{o_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(112))
			{p_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(113))
			{q_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(114))
			{r_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(115))
			{s_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(116))
			{t_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(117))
			{u_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(118))
			{v_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(119))
			{w_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(120))
			{x_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(121))
			{y_count ++;
				inFile >> letter;
			}
			else if (letter = static_cast<int>(122))
			{z_count ++;
				inFile >> letter;
			}
			else {
				inFile >> letter;
			}	
							}
		}


    inFile.close();
     

    outFile << a_count << " a" << endl << b_count << " b" << endl << 
	c_count << " c" << endl << d_count << " d" << endl << 
	e_count << " e" << endl << f_count << " f" << endl << 
	g_count << " g" << endl << h_count << " h" << endl << 
	i_count << " i" << endl << j_count << " j" << endl << 
	k_count << " k" << endl << l_count << " l" << endl << 
	m_count << " m" << endl << n_count << " n" << endl << 
	o_count << " o" << endl << p_count << " p" << endl << 
	q_count << " q" << endl << r_count << " r" << endl << 
	s_count << " s" << endl << t_count << " t" << endl << 
	u_count << " u" << endl << v_count << " v" << endl << 
	w_count << " w" << endl << x_count << " x" << endl << 
	y_count << " y" << endl << z_count << " z" << endl;
    return 0;
    }


I'm sure there are prettier ways of doing this but I dont know how, thanks in advance
Use '==' in if statements. The first if statement (testing for 'a') always returns true because you used a single equal sign and all the others are skipped.

By the way, have you learned about arrays yet? An array could very greatly simplify this program. You could do something like this:

1
2
3
4
5
while(!inFile.eof())
{
    inFile >> letter;
    letterCount[letter - 97]++;
}


Instead of all of those if statements.
Last edited on
Thanks for the reply Pyrius, it didnt occur to me to make it into an array. But now having made those changes im at a lost for calculating the percentages for each letter. At the moment all the values return 0.00000

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

	
    char letter;
	int myarrayalpha[26];
	int total_letters;
	void calculatetotal (const int[], int&);
	void calculatepercentage (const int[], float[], int);
	float percentage[26];



	// start function
	int main() 
{
	
	ifstream inFile;
	ofstream outFile;

	inFile.open ("code.txt");
	outFile.open ("results.txt");

	inFile >> letter;

	// if cant find file display error
	if (!inFile)
	{
		cout << "Cant find file input ";
											
	}

	
	// else the program starts
	else 
	{
   		while (!inFile.eof()) 
		{
			myarrayalpha[letter - 97]++;
			inFile>>letter;
		}
		
		// print outputs
		outFile << "Total: " << total_letters << endl;
		for (letter=0; letter<26; letter++)
		{
			outFile << "Total " << (char) (97 + letter) << "'s :" << fixed << setw(3) << myarrayalpha[letter] <<
			"	Percentage " << (char) (97 + letter) << "'s :"<< fixed << setw(3) << percentage[letter] << "%" << endl; 
		}
		
	}
	

    inFile.close();
    outFile.close();
	return 0;
}

	

void calculatetotal (const int myarrayalpha[], int& total_letters)
{for (letter=0; letter<26; letter++)
	total_letters = myarrayalpha[letter] + total_letters;
}

void calculatepercentage (const int myarrayalpha[], float percentage[], int total_letters)
{for (letter=0; letter<26; letter++)
	percentage[letter] = myarrayalpha[letter]/total_letters*100.00;
}
Last edited on
For percentages:

( current letter count / total letter count ) * 100

( 25 'a's / 200 char count( total ) ) * 100 = 12.5%
I'm fairly sure thats what I have done however I'm starting to think its to do with my float and int variables
You shouldn't name an array "array", because if you were to skim through your code it would be confusing as to what the variable array is for. Give it a name relevant to what it's used for. Also, what are the "#include <iomanip>", "fixed", "setw(3)" and "int number" for?

To solve your problems with the percentages, put (float) in front of array[letter]. You could also make the program return 0 if the file can't be opened instead of putting the rest of the program in an else block.

EDIT: One more thing: you don't need to pass global variables to functions as parameters.
Last edited on
Thanks again Pyrius, I'll try and tidy up the code. As far as im aware the "#include <iomanip> allows editing of the output, hence the "fixed" and "setw(3)" basically makes the output look nice, similar to setprecision().And the "int number" was for something else I was trying to do.

Thanks again problem solved.
Topic archived. No new replies allowed.