Not keeping track of lowercases ?

I am practicing on a program where i have to keep track on how many lower cases there are but i keep getting zero cant seem to find the problem

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
  #include <iostream>
#include <cstring>
using namespace std;
void checking (char[]);

int main ()
{
	char *arr;
	char buffer[80];
	int result;
	cout <<"Enter string now :";
	cin.getline(buffer, 80);
	arr = new char[strlen(buffer)+1];
	strcpy(arr,buffer);
	checking (arr);
	return 0;
}
void checking (char ar[])
{
	int lower = 0;
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x] = 0))
			lower++;
	}
	
	cout <<" Number of lower cases : " << lower << endl;
}

Last edited on
The error is on line 23. Use ==, and not =.
mlholder is correct about using == instead of =.

But even with that change, the program won't work because your parentheses are not placed properly. I suggest you replace that line (line 23) with the following line:

1
2
if (islower(ar[x]))
    lower++;


The function 'islower' returns a bool i.e 'true' or 'false'.
So you don't need to check if it equals 0 or not. You can just leave it like I told you and the compiler knows that it has to check if it is true or not.
i cant believe i didnt catch that small mistake the last program i was doing was doing it to true or false and i guess it got mix with this one but yes i tried doing what you said newbiee999 and it totally works now !!! thank you so much :D

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
#include <iostream>
#include <cstring>
using namespace std;
void checking (char[]);

int main ()
{
	char *arr;
	char buffer[80];
	int result;
	cout <<"Enter string now :";
	cin.getline(buffer, 80);
	arr = new char[strlen(buffer)+1];
	strcpy(arr,buffer);
	checking (arr);
	return 0;
}
void checking (char ar[])
{
	int lower = 0;
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x]))
			lower++;
	}
	
	cout <<" Number of lower cases : " << lower << endl;
}
Last edited on
Topic archived. No new replies allowed.