Error: Expression must have class type

Help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char phrase[1000], ltr;


int main()
{
	cout << "Enter a string: ";
	cin.getline(phrase, 1000);
	cout << "Enter character to search: ";
	ltr = isalpha(phrase[1000]);
	cin >> ltr;
	cout << "The given string is: " << phrase;
	cout << "\nGiven character is " << ltr;
	cout << "\nPresent number of times: " << count(phrase[1000].begin(), phrase[1000].end(), ltr);//here's the error


	system("pause>0");
	return 0;
}
They syntax phrase[1000].function_call() is only valid if the variable phrase[1000] is an object of a function. phrase[1000] is of type char and phrase itself is of type char* which are primitive data types which are certainly not classes.

I think the root of your problem is you've confused strings and cstrings. begin() and end() are functions of the class string.

Strings: http://www.cplusplus.com/reference/string/string/
I tried ltr.size and still won't work... What is the best substitute of this? I wanted to get the total number of a certain character. Thank you :)
strlen() is used to get the length of a string:

http://www.cplusplus.com/reference/cstring/strlen/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	cout << "Enter a string: ";
	cin.getline(phrase, 1000);
	cout << "Enter character to search: ";
	ltr = isalpha(phrase[1000]);
	cin >> ltr;
	cout << "The given string is: " << phrase;
	cout << "\nGiven character is " << ltr;
	cout << "\nPresent number of times: " << isalpha(strlen(phrase));
	//for (int i = 0; i < strlen(phrase); i++){
	//		cout << isalpha(phrase[i]);
	//}

	system("pause>0");
	return 0;
}

well it outputs 0 even though it's 2
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
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

char ltr;
char phrase[1000];
int countChars(char *, char);


int main()
{
	int count = 0;
	cout << "Enter a string: ";
	cin.getline(phrase, 1000);
	cout << "Enter character to search: ";
	cin >> ltr;
	cout << "The given string is: " << phrase;
	cout << "\nGiven character is " << ltr;
	cout << "\nPresent number of times: " << countChars(phrase, ltr);


	system("pause>0");
	return 0;
}

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

I fixed this :P
Topic archived. No new replies allowed.