Seeking help with my program!

I am writing a program that prompts a user to enter a sentence and then has a function that counts the letters in that sentence. For instance it will display how many a's, b's, c's, d's, etc. Also it will count numbers and the total characters including spaces. I have it compiling as of now and the input function and the quit option are working correctly but I am struggling with the counting function. Anyone have any ideas?

Thanks in advance.

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

void Input(char* inbuf, int& count);
void CountAndDisplay(char* inbuf, int& count);

const int SIZE = 132;

int main()
{
	char inbuf[SIZE];
	int count;

	char loop = 'y';
	char menu;

	while (loop == 'y')
	{
		cout << "Main Menu" << endl;
		cout << "(E)nter the sentence to process" << endl;
		cout << "(C)ount the letters and then display them" << endl;
		cout << "(Q)uit the program" << endl;
		cout << "Enter either (E), (C) or (Q)" << endl;
		cin.get(menu);

		switch (menu)
		{
		case 'e':
		case 'E':
			Input(inbuf, count);
			system("cls");
			break;
		case 'c':
		case 'C':
			CountAndDisplay(inbuf, count);
			system("cls");
			break;
		case 'q':
		case 'Q':
			loop = 'n';
			break;
		}
	}

	system("cls");
	return 0;
}

void CountAndDisplay(char* inbuf, int &count)
{
	int i = 0;
	short CharCountBuf[127];
	short charval;

	for (int i = 0; i < count; i++)
	{

		if ((inbuf[i] >= 'a') && (inbuf[i] <= 'z'))
		{
			charval = (short)'a';
			if (charval == (short)inbuf[i])

				CharCountBuf[0 + charval] += 1;
		}
	}

	cout << CharCountBuf << " - " << (char)CharCountBuf[0 + i] << endl;

	system("pause");
}

void Input(char* inbuf, int& count)
{
	cout << "Please enter a sentence: " << endl;
	cin.ignore();
	cin.getline(inbuf, SIZE);
	count = cin.gcount();
	system("pause");
}
Is there a reason you are using C-strings instead of C++ strings?

To get the length of a C-string you probably should use the strlen() function from <cstring>.

I was just told to do it this way for the assignment. Not sure why :/
Bump!
Nobody has any ideas?
An ASCII table has 127 decimal values. This is why your CharCountBuf is 127 elements (see http://www.asciitable.com/).

First convert inbuf[i] to a short, similar to what you do on line 60... but do it to the actual character, not just 'a'. Once you have done that, the if condition on line 58 probably just needs to check that the short you have representing that character is between 0 and the size of your array. If it is, you can increment the "charval" element in the array. Think of it this way - each element in your array is a character, and charval is the character's index in that array.

Once you've done that, you have successfully counted all the characters.

If you need to count the total characters (assuming some characters in your string might have been ignored due to not being an ASCII character), you can add all the elements in your array together to get the total number of ASCII characters.
tscott8706,
Thank you so much for the response. I really appreciate the help. I've read your response and when I get home today I will be working on it and I'll post with what progress I've made!
why u dont try with a for loop?

for(int b=0;sentence[b];b++)contador++;


and for the search of the letter why u dont try a double for loop?

for( "this one will display the letters")
for( "this one will display the letters in the input sentence") (condition);


tscott8706,

I'm trying to implement what you're saying but I'm still having difficulty unfortunately. Is there anywhere you could write some of it out so I can visually see what you're saying to do?

Thanks again
Update:

It's just dawned on me that what you are saying is counting the number of characters in the user's sentence. I need to count and display how many of EACH character and how many of EACH number are in the sentence. For example how many "a's", how many "b's", how many "c's", and so on.
i mean something like this :

char a[100];
char characters;
int counter;


counter=0;
cout<<"Write the sentence:"<<endl;

gets(a);

for(int z=0;a[z];z++)a[z]=tolower(a[z]);

for(int b=97;b<123;b++){
for(int z=0;a[z];z++)
{
characters =(char)b;
if(characters==a[z])counter++;
}
cout<<characters<<": "<<counter<<endl;
counter=0;

}
Edevanx,

Thanks for the input! I'll look into that!
Bump for anyone else that may have a good idea!
@jmag91

What tscott8706 described did end up with counting the number of characters in the sentence, but you could easily change the end to output how many of each character was counted.

This is what I mean, and sort of like what tscott8706 described:

1
2
3
4
5
6
7
8
9
10
11
12
13
void CountAndDisplay(char* inbuf, int &count)
{
    short CharCountBuf[128] = {0}; //ASCII values 0 to 127 is a total of 128 values

    for (int i = 0; i < count; i++)
        if ((inbuf[i] < 128)
            ++CharCountBuf[inbuf[i]];
    for (char c = 'a'; c <= 'z'; ++c)
        cout << c << " - " << CharCountBuf[(short)c] << endl;
    for (char c = 'A'; c <= 'Z'; ++c)
        cout << c << " - " << CharCountBuf[(short)c] << endl;
    system("pause");
}
What fg109 said. I think that answers your question, does it not? You could also include '0' - '9' if you needed the numbers.
And it's working!!!!!!!

Thank you SO much for your help you guys. I appreciate it so incredibly much. You guys are awesome. :)
Topic archived. No new replies allowed.