Help with adding the occurence of each read character

Hello, I'm having trouble displaying the number of times a character has been read from a file. for example "Big black dog" b = 2. It reads all the letters but does not sum it up. Thanks in advance. the filename has three lines and reads:

The quick brown fox jumps over the lazy dog!
1122234567899990
C++ Programming is fun!

#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

void openInputFile(ifstream &, string str);
void alphaSort(char [], int);
int countChars(char *, char);
int main()
{
char ch;
ifstream infile;
int k = 0;
int l = 0;
int p = 0;
int size;
const int ALPHA_SIZE = 26;
const int NUM_SIZE = 10;
char arrAlphaPtr[ALPHA_SIZE] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char alphaArr[100];
char numArr[100];
char puncArr[5];
string inFileName = "filename";

openInputFile(infile, inFileName);

infile >> ch;
while (infile)
{
if (isalpha(ch))
{
ch = tolower(ch);
alphaArr[k++] = ch;
}
else if (isdigit(ch))
{
numArr[l++] = ch;
}
else if (ispunct(ch))
{
puncArr[p++] = ch;
}
infile >> ch;
}

infile.close();
cout << endl;


alphaSort(alphaArr, k);


for (int j = 0; j < k; j++)
{
cout << arrAlphaPtr[j] << ": " << countChars(alphaArr, j ) << "\t";

}

cout << endl;

for (int h = 0; h < l; h++)
{
cout << numArr[h];
}
cout << endl;

for (int g = 0; g < p; g++)
{
cout << puncArr[g];
}
return 0;
}

void openInputFile (ifstream &infile, string inFileName)
{
if (!infile)
{
cout << " Error, file does not exist!";
exit(12);
}
else
infile.open("filename");
}
void alphaSort(char alphaArr[], int k)
{
int alphaScan, minIndex;
double minElem;


for (alphaScan = 0; alphaScan < (k - 1); alphaScan++)
{
minIndex = alphaScan;
minElem = alphaArr[alphaScan];
for (int index = alphaScan + 1; index < k; index++)
{
if (alphaArr[index] < minElem)
{
minElem = alphaArr[index];
minIndex = index;
}
}
alphaArr[minIndex] = alphaArr[alphaScan];
alphaArr[alphaScan] = minElem;
}
}
int countChars(char *strPtr, char ch)
{
int times = 0;

while (*strPtr != '\0')
{
if (*strPtr == ch)
times++;
strPtr++;
}
return times;
}
If you don't mind some changes,
Here is an example for what you're trying to achieve.
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
#include <iostream>
#include <fstream>
#include <map>

using namespace std;

int main(){

	ifstream ifs;
	map<char, int> numOfAlpha;

	ifs.open("filename");

	char c = ifs.get();

	while(ifs.good()){
		++numOfAlpha[c];
		c = ifs.get();
	}

	for(map<char, int>::iterator it = numOfAlpha.begin(); it != numOfAlpha.end(); ++it){
		char character = it->first;
		int numOfCharacter = it->second;

		if(character == '\n'){
			cout << "'" << "\\n" << "' = " << numOfCharacter << "\n";
		}else{
			cout << "'" << character << "' = " << numOfCharacter << "\n";
		}

	}
	ifs.close();
}


The quick brown fox jumps over the lazy dog!
1122234567899990
C++ Programming is fun!
'\n' = 3
' ' = 11
'!' = 2
'+' = 2
'0' = 1
'1' = 2
'2' = 3
'3' = 1
'4' = 1
'5' = 1
'6' = 1
'7' = 1
'8' = 1
'9' = 4
'C' = 1
'P' = 1
'T' = 1
'a' = 2
'b' = 1
'c' = 1
so on . . .


You can extract just the alphabet's count to fit your needs.
Hello Arzhon, I'm amazed at your level of expertise and some day ill achieve that level. Even though that is exactly what I want it to do, instructions ask to use functional decomposition and arrays for both alpha and numeric characters. Hopefully you can assist me with that and I appreciate your help.
Hello Miglr, I'm impressed by your attitude towards learning, I'm sure you will do great.

At the moment, I noticed that you have actually solved your problem, just a minor typo at this function calls countChars(alphaArr, j ), I'm guessing you were planning to do this instead countChars(alphaArr, arrAlphaPtr[j])

Also, you'd want to loop from j = 0 as long as j < ALPHA_SIZE, not j < k

The same goes for the next two loops.
Last edited on
@arzhon
Thank You my friend! That did it and is actually taking each count. One more question if you don't mind?
I'm getting extra readings once it goes beyond the letter z. How can I make it stop once I reach the last letter of the alphabet? When I try changing the size of the array to only take 26 elements, the file does not completely read. Is there a way on fixing that?
No problem (y), oh for that, I just modified your code to get it working:

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
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;

void openInputFile(ifstream & , string str);
void alphaSort(char[], int);
int countChars(char * , char);

int main() {
  char ch;
  ifstream infile;
  int k = 0;
  int l = 0;
  int p = 0;
  int size;
  const int ALPHA_SIZE = 26;
  const int NUM_SIZE = 10;
  char arrAlphaPtr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  char arrDigitPtr[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
  char arrPuncPtr[] = {'?', '+', '!', '.', ','};
  char alphaArr[100];
  char numArr[100];
  char puncArr[5];
  string inFileName = "filename";
  openInputFile(infile, inFileName);

  infile >> ch;
  while (infile) {
    if (isalpha(ch)) {
      ch = tolower(ch);
      alphaArr[k++] = ch;
    } else if (isdigit(ch)) {
      numArr[l++] = ch;
    } else if (ispunct(ch)) {
      puncArr[p++] = ch;
    }
    infile >> ch;
  }

  infile.close();
  cout << endl;

  alphaSort(alphaArr, k);

  for (int j = 0; j < ALPHA_SIZE; j++) {
    cout << arrAlphaPtr[j] << ": " << countChars(alphaArr, arrAlphaPtr[j]) << "\t";
  }

  cout << endl << endl;

  for (int h = 0; h < NUM_SIZE; h++) {
    cout << arrDigitPtr[h] << ": " << countChars(numArr, arrDigitPtr[h]) << "\t";
  }

  cout << endl << endl;

  for (int g = 0; g < p; g++) {
    cout << "'" << arrPuncPtr[g] << "': " << countChars(puncArr, arrPuncPtr[g]) << "\t";
  }

  cout << endl;
  return 0;
}

void openInputFile(ifstream & infile, string inFileName) {
  if (!infile) {
    cout << " Error, file does not exist!";
    exit(12);
  } else
    infile.open("filename");
}
void alphaSort(char alphaArr[], int k) {
  int alphaScan, minIndex;
  double minElem;

  for (alphaScan = 0; alphaScan < (k - 1); alphaScan++) {
    minIndex = alphaScan;
    minElem = alphaArr[alphaScan];
    for (int index = alphaScan + 1; index < k; index++) {
      if (alphaArr[index] < minElem) {
        minElem = alphaArr[index];
        minIndex = index;
      }
    }
    alphaArr[minIndex] = alphaArr[alphaScan];
    alphaArr[alphaScan] = minElem;
  }
}
int countChars(char * strPtr, char ch) {
  int times = 0;

  while ( * strPtr != '\0') {
    if ( * strPtr == ch)
      times++;
    strPtr++;
  }
  return times;
}


a: 2	b: 1	c: 2	d: 1	e: 3	f: 2	g: 3	h: 2	i: 3	j: 1	k: 1	l: 1	m: 3	n: 3 
o: 5	p: 2	q: 1	r: 4	s: 2	t: 2	u: 3	v: 1	w: 1	x: 1	y: 1	z: 1	

0: 1	1: 2	2: 3	3: 1	4: 1	5: 1	6: 1	7: 1	8: 1	9: 4	

'?': 0	'+': 2	'!': 2	'.': 0	


ps:
http://www.tutorialspoint.com/online_c_formatter.htm
you may want to use this code beautifier if
function()
{

}

is more of your style :)
Last edited on
@arzhon
Thank you so much I really appreciate it! As I was reading the code I realized that I had the wrong tester value in my expression. I'm proud of myself that there were minor changes. if you only knew the frustration I was going through to get this done. Thanks again my friend... I can finally sleep at peace tonight.


Thanks for the link... Its going to come in handy
Last edited on
Topic archived. No new replies allowed.