displaying the most and least occurrences of letters from a .txt file

I have a program that I am writing where I have to count how often a letter appears from a .txt file. I got that done and was able to write the code for the program to display the letter that occurs the most and how many times it does, which in this case is (E,147) my problem comes when I try to find and display the letter that occurs the least amount of times. I have tried to find my mistake as I feel it is something simple and I am just missing it but I cannot for the life of me figure it out. Any help is appreciated

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
 #include <iostream>;
#include <fstream>;
#include <string>;



using namespace std;







int main()
{
	
	char LetterInFile;
	int total[26] = { 0 };


	ifstream infile("letter_count.txt");


// make sure it can open
	if (!infile)                                   
	{
		cout << endl << "Unable to open text file";



		return 0;
	}

	while (infile.get(LetterInFile))
	{
		if (isalpha(LetterInFile))
		{
            // convert
			LetterInFile = toupper(LetterInFile);
			int index = LetterInFile - 'A';
			total[index]++;
		}
	}



////////////////// /////headers///////////////////////////
	cout << "Letter" << "       Appearances" <<   endl;//
	cout << "========================" << endl;        //
////////////////////// headers///////////////////////////

	

		int HighestElement = 0;
		int HighestValue = 0;

		for (int index = 0; index < 26; index++)
		{
			if (total[index] > HighestValue)
			{
				HighestValue = total[index];
				HighestElement = index;
			}
		}
		
	// HighestElement = the postion of highest number

	
		int LowestElement = 0;
		int LowestValue = 0;

		for (int index = 0; index < 26; index++)
		{
			if (total[index] < LowestValue)
			{
				LowestValue = total[index];
				LowestElement = index;
			}
		}

		// LowestElement equals the postion of the lowest value letter





		cout << "" <<  char(HighestElement + 'A') << "              " << total[HighestElement] << endl;
		cout << "" << char(LowestElement + 'A') << "              " << total[LowestElement] << endl;
}



1
2
3
		int LowestValue = 0;

		if (total[index] < LowestValue)		

Just a simple logic error. How can any counter be less than 0? That is, you can't have a negative count of something, which is why you get an incorrect least value. I'd recommend you initialise LowestValue with, say, the first element in the total array, like so:
int LowestValue = total[0];
Likewise with your HighestValue variable.
To think all my trouble and going over and over it was all due to a simple logic error. Thank you for helping me pinpoint the issues. Now it runs perfectly with no problems.
Topic archived. No new replies allowed.