getting program to end loop and count properly

Hello, I am a new student learning C++ and I am having trouble with my program for a class that uses 2 arrays to count the number of upper and lower case letters in a String.

the first problem I am having is exiting the loop. I am unsure why it is not taking my sentinel value (which would be "End It")

the second problem I am having is counting the letters in the word properly. It counts how many times each letter occurs, but in the end it counts the actual amount of letters in the word improperly

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
  #include <iostream>
#include <cstring>
using namespace std;
void InputWord(char Word[]);
void CountOccurrences(char Word[], char Letter[], int LetterCount[]);
int main()
{
	char Word[60];
		do
		{
		
		char Letter[52] = { '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', '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' };

		int LetterCount[52] = { 0 };
		
				cout << " LETTER COUNTS " << endl;
				cout << "************************************************" << endl;
				InputWord(Word);
				
					cout << "------------------------------------------------" << endl;
					CountOccurrences(Word, Letter, LetterCount);
					cout << "************************************************" << endl;
					cout << endl;
					InputWord(Word);
					
				}while (Word != "End It");
	return 0;
}
void InputWord(char Word[])
{
    cout << "Enter the word to be letter counted: ";
	cin >> Word;	
}

void CountOccurrences(char Word[], char Letter[], int LetterCount[])
{
	int Index = 0;
	while (Word[Index] != '\0')
	{
		if (Word[Index] >= 'A' && Word[Index] <= 'Z' || Word[Index] >= 'a' && Word[Index] <= 'z')
		{
			for (int Count = 0; Count < 52; Count++)
			{
				if (Word[Index] == Letter[Count])
				{
					LetterCount[Count] = LetterCount[Count] + 1;
				}
			}
		}
		Index++;
	}

	int CharCount = 0;
	for (int Index = 0; Index < 52; Index++)
	{
		if (LetterCount[Index] >= 1)
		{
			cout << "There are " << LetterCount[Index] << " " << Letter[Index] << "'s"
				<< endl;
			CharCount++;
		}
	}

	cout << "------------------------------------------------" << endl;
	cout << "There is a total of " << CharCount << " characters in the word " << Word << endl;

}
Line 30: Word is a c string. What you do is comparing the pointers which are different. So either use strcmp(...) for the c string or better use std::string as the type of Word. Then you can use != for comparison.
thank you for your help. How would go about using that function? thanks in advanced
I keep getting an error code when using != int the strcmp. not quite sure why. any help would be appreciated. thanks.


#include <iostream>
#include <cstring>
using namespace std;
void InputWord(char Word[]);
void CountOccurrences(char Word[], char Letter[], int LetterCount[]);
int main()
{
char Word[60];
char End[] = "End It";
do
{
if (strcmp(Word!=End))
{
char Letter[52] = { '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', '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' };
int LetterCount[52] = { 0 };

cout << " LETTER COUNTS " << endl;
cout << "************************************************" << endl;
InputWord(Word);
cout << "------------------------------------------------" << endl;
CountOccurrences(Word, Letter, LetterCount);
cout << "************************************************" << endl;
cout << endl;
}
} while (Word);
return 0;
}
void InputWord(char Word[])
{
cout << "Enter the word to be letter counted: ";
cin >> Word;
}

void CountOccurrences(char Word[], char Letter[], int LetterCount[])
{
int Index = 0;
while (Word[Index] != '\0')
{
if (Word[Index] >= 'A' && Word[Index] <= 'Z' || Word[Index] >= 'a' && Word[Index] <= 'z')
{
for (int Count = 0; Count < 52; Count++)
{
if (Word[Index] == Letter[Count])
{
LetterCount[Count] = LetterCount[Count] + 1;
}
}
}
Index++;
}
int Length;
Length = strlen(Word);
int CharCount = 0;
for (int Index = 0; Index < 52; Index++)
{
if (LetterCount[Index] >= 1)
{
cout << "There are " << LetterCount[Index] << " " << Letter[Index] << "'s"
<< endl;
CharCount++;
}
}

cout << "------------------------------------------------" << endl;
cout << "There is a total of " << Length << " characters in the word " << Word << endl;

}
I keep getting an error code when using != int the strcmp
That's not how the strcmp(...) is used. See:

http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp

You need to provide the c strings as arguments and check the result. See the Example.
Topic archived. No new replies allowed.