counting occurrences of alphabets

MY hw assignment is to write a program that counts the number of letter occurrences
im required to use the 2 functions: charcount and countout

my question is my if stmt in CharCount function is kinda iffy
and my output is gibberish as well, sometimes outputs stuff in hex and sometimes outputs in characters that ive never seen

and im honestly stuck on where i am

if anyone would be so kind to point out my mistakes and point me in the right direction on where i should go

this is what i have so far

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
#include <iostream>
using namespace std;

struct counting
{
public:
void CharCount(char num[], int size); //function that counts the number of letter & number occurrences
void CountOut(); /*function that outputs 
					a is 6 times
					b is 4 times
					c is 0 times
					etc... */
char count[132];
};

void counting::CharCount(char num[], int size)
{
	// will be using 3 for loops here that address each of the 3 groups of alphanumeric (a-z & A-Z & 0-9)
	int i = 0, j = 0;
	counting abc;
	abc.count[132];
	for (i = 0; i < size; i++)  //iterate thru every address in the array
	{
		abc.count[i] = 0;

		if(num[j] >= 'a' && num[j] <= 'z')  //checking the array if it is a letter
	
	        {
			//if it is, mark that letter down x amount of times for every alphabet 
			abc.count[ num[j] ]++;  //checking every element in the array for the letter and adding it to the counter?
		}
	}
}

void counting::CountOut()
{
	// function that handles the output
	counting abc;

	for (char ch = 'a'; ch <= 'z'; ch++)
	{
		cout << "character: " << ch << " appeared " << abc.count << " times." << endl;
	}
}

int main()
{
	char CharInput[132]; //array length of 0-131
        counting occur;

	cout << "input a line of text" << endl;
	cin >> CharInput; //asked user to iput line of text

	occur.CharCount(CharInput, sizeof(CharInput)); //passing string to function to count letters & numbers)

	occur.CountOut();

	system("pause");
	return 0;
}
just out of curiousity, is the point to learn oop with this? if not i wouldnt wrap it in a class. anyways, why are you creating an instance of an object in your method? replace abc with *this and it might work out better. i cant investigate further right now unfortunately.
it's so that i could use abc to access the functions and using it to make count a pass-by-value, then i could pass it from CharCount to CountOut

the CountOut function has to output the program results
and i didnt know any other way to call a function from another function other than using global variable or pass by value

and i cant use global variables (also one of the requirements)
you can just call the function. either remove the abc. or replace it with *this. its why you are getting junk values i think
remove both abc in the functions and and make count pass by reference?

i tried that before i resorted to struct

when i call CharCount in CountOut by
1
2
3
4
5
6
7
8
9
void CountOut()
{
     CharCount(char num[], int size);
    
for (char ch = 'a'; ch <= 'z'; ch++)
	{
		cout << "character: " << ch << " appeared " << count << " times." << endl;
	}
}


i get undeclared variable on count, num
and unexpected int on size
compile error on []
Last edited on
Topic archived. No new replies allowed.