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
#include <iostream>
usingnamespace 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)