counting characters

hi! i'm still fresh in c++
i'm given a homework by my tutor on counting the characters of 'a' , 'x' , ' ' (space), and other characters when the user input 20 characters.
he has given some tips on solving this but still i'm stuck with it...
here's my progress........

#include <iostream>
using namespace std;
int main ()
{
char c[20];
int count_a, count_x, count_space, count_other;
cout<<"enter 20 characters (press enter after each character) : ";

for (int i=0; i<20; i++)
{
cin >>c;
if (c == 'a')
{
count_a++;
}
else
{
if (c == 'x')
{
count_x++;
}
else
{
if (c == ' ')
{
count_space++;
}
else
{
count_other++;
}
}
}
}


cout<<"\n\na count :"<<count_a;
cout<<"\nx count :"<<count_x;
cout<<"\nspace count :"<<count_space;
cout<<"\nother count :"<<count_other;
cout<<endl;

system ("pause");
return 0;
}

the system keeps on sending the message - 'ISO C++ forbids comparison between pointer and integer'
what should i do?

Since your variable named "c" is an array, you need to use array syntax in your comparisons:

1
2
3
4

if( c[ 0 ] == 'a' )

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
//find the space in the character . 

#include <iostream>
using namespace std;
int main ()
{ 
	char c[20]  = {0};
	int count_a = 0 , count_x = 0, count_space = 0, count_other = 0;
	cout<<"enter 20 characters (press enter after each character) : ";
	cin >>c;
		for (int i=0; i<20; i++)
		{
			
			if (c[i] == 'a')
			{
				count_a++;
			}
			else if (c[i] == 'x')
				{
					count_x++;
				}else if (c[i] == ' ')
					{
						count_space++;
					}
					else
					{
						count_other++;
					}
					
			
		}


cout<<"\n\na count :"<<count_a;
cout<<"\nx count :"<<count_x;
cout<<"\nspace count :"<<count_space;
cout<<"\nother count :"<<count_other;
cout<<endl;

system ("pause");
return 0;
}
OKOK!!
thanks everyone...=))
Topic archived. No new replies allowed.