Keeps returning false, when it needs to be true

So I am practicing on my program on returning on false or true but i am having issue with the returning it keeps returning false where it needs to be true not sure how I am writing it wrong in the function please help

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

int main ()
{
	char * arr;
	char buffer [80];
	int result;
	cout <<"PLease enter  password for validation now : ";
	cin.getline(buffer,80);
	arr = new char [strlen(buffer)+1];
	strcpy(arr,buffer);
	checking (arr);
	result = checking (arr);
	if (result == true)
	cout <<"There are lower cases  \n";
	else if (result == false ) 
		cout <<"There are no lower cases \n";
	return 0;
}
bool checking (char ar[])
{
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x] != 0))
			return true;
		else if (islower(ar[x] = 0))
			return false;
	}
}
1
2
else if (islower(ar[x] = 0))
			return false;

Remember to use '==' and not '='. The latter is assignment. The former is like a comparison.

I don't understand why would you compare it to 0. "islower()" function checks to see if there are lower cases. You can just remove the "!= 0" altogether and the code should work. Try running this.
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
 #include <iostream>
#include <cstring>
using namespace std;
bool checking (char []);

int main ()
{
	char * arr;
	char buffer [80];
	int result;
	cout <<"PLease enter  password for validation now : ";
	cin.getline(buffer,80);
	arr = new char [strlen(buffer)+1];
	strcpy(arr,buffer);
	checking (arr);
	result = checking (arr);
	if (result == true)
	cout <<"There are lower cases  \n";
	else if (result == false ) 
		cout <<"There are no lower cases \n";
	return 0;
}
bool checking (char ar[])
{
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x]))
			return true;
		else if (!islower(ar[x])) //Note where I put the '!'
			return false;
	}
}


Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
bool checking (char ar[])
{
	for (int x = 0; x < strlen(ar);x++)
	{
		if ( islower(ar[x]) != 0 )
			return true;
		else 
			return false;
	}
}
Last edited on
I have tried all what you guys suggested but still not getting any result though if the first letter isnt a lower case then it would give me a fail return that is the issue i am running into
return instantly ends the function there and returns that value; you never check past the first value in the array. What you want to do, I think, it return true only if you find a lowercase letter, and to return false only after having gone through the entire array and not having returned true.
Yea that is the issue i am having it seems the program is only reading the first value and stops there can not recall how to make it read all the other ones
so i ended up doing this and it seems to work any other way you guys think this can go ??

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

int main ()
{
	char * arr;
	char buffer [80];
	int result;
	cout <<"PLease enter  password for validation now : ";
	cin.getline(buffer,80);
	arr = new char [strlen(buffer)+1];
	strcpy(arr,buffer);
	checking (arr);
	result = checking (arr);
	if (result == true)
		cout <<"Caught a lowercase \n";
	else if ( result == false)
		cout <<"Did not catch any lowercases \n";
	return 0;
}
bool  checking (char *ar)
{
	int lowercase = 0;
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x]))
			lowercase++;
	}
	if (lowercase > 0)
		return true;
	else 
		return false;

}
Last edited on
As your code is basically looking for at least one lowercase char you can code this as

1
2
3
4
5
6
7
8
9
10
bool checking (char *ar)
{
	int len = strlen(ar); // call strlen() just once!
	for (int x = 0; x < len; x++)
	{
		if (islower(ar[x]))
			return true; // jump out of loop as soon as find first lowercase char
	}
	return false;
}


Andy

PS A couple of points

1. Your code is leaking memory! (see comments)

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

int main ()
{
	char * arr;
	char buffer [80];
	int result;
	cout <<"PLease enter  password for validation now : ";
	cin.getline(buffer,80);
	arr = new char [strlen(buffer)+1]; // ALLOCATE
	strcpy(arr,buffer);
	//checking (arr); REMOVE POINTLESS CALL
	result = checking (arr);
	if (result == true)
		cout <<"Caught a lowercase \n";
	else if ( result == false)
		cout <<"Did not catch any lowercases \n";
        delete [] arr; // AND FREE!!! (was missing)
	return 0;
}

bool checking (char *ar)
{
	int len = strlen(ar); // call strlen() just once!
	for (int x = 0; x < len; x++)
	{
		if (islower(ar[x]))
			return true; // jump out of loop as soon as find first lowercase char
	}
	return false;
}


2. You can pass a char array to a function that takes a char* so you don't need the new (and delete) anyway.

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
#include <iostream>
#include <cstring>
using namespace std;
bool  checking (const char *); // safer to use const char*

int main ()
{
	char buffer [80] = {0}; // fill buffer with zeros for safety
	//int result; now declared at point of use
	cout <<"Please enter  password for validation now : ";
	cin.getline(buffer,80);
	int result = checking (buffer); // pass buffer to function
	if (result) // no need to test against true explictly
		cout <<"Caught a lowercase \n";
	else // no need for test against false at all, as if it's not true...
		cout <<"Did not catch any lowercases \n";
	return 0;
}

bool checking (const char *ar) // safer to use const char*
{
	const int len = strlen(ar); // call strlen() just once! (const as doesn't change)
	for (int x = 0; x < len; x++)
	{
		if (islower(ar[x]))
			return true; // jump out of loop as soon as find first lowercase char
	}
	return false;
}

Last edited on
closed account (48T7M4Gy)
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
#include <iostream>
#include <cstring>
using namespace std;
bool  checking (const char *); // safer to use const char*

int main ()
{
	char buffer [80] = {0};
	cout <<"Please enter  password for validation now : ";
	cin.getline(buffer,80);
	
	if (checking (buffer))
		cout <<"Caught a lowercase \n";
	else
		cout <<"Did not catch any lowercases \n";
	return 0;
}

bool checking (const char *ar)
{
	bool result = false;
	
	for (int x = 0; x < strlen(ar); x++)
	{
		if (islower(ar[x]))
			return true;
	}
	return false;
}

This takes it a small step further
BTW you do get why your original code didn't run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool checking (char ar[]) // this should have take a const char*!
{
        // the first time the loop is called strlen(ar) evaluates to a sensible value
        // but the second time it evaluates to 0 as you set the first char to 0 (the
        // null terminator)
	for (int x = 0; x < strlen(ar);x++)
	{
		if (islower(ar[x] != 0)) // tests if tolower() doesn't return the null
			return true;     // char (0 is same as '\0') -- which only exists
                                         // at index strlen(ar), so will be false for first char)
		else if (islower(ar[x] = 0)) // operator= sets element to 0 (or '\0') and 
			return false;        // returns 0 (the value set), so if is valuated
                                             // against 0 which is same as false
	}
}
Last edited on
thanks guys this really help me out on this program and thank you andywestken i finally caught what i was doing wrong :D

And thanks for reminding about the delete part i always forget to put that in there
closed account (48T7M4Gy)
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
#include <iostream>
#include <cstring>
using namespace std;
bool  checking (const char *); // safer to use const char*

int main ()
{
	char buffer [80] = {0};
	cout <<"Please enter  password for validation now : ";
	cin.getline(buffer,80);
	
	if (checking (buffer))
		cout <<"Caught a lowercase \n";
	else
		cout <<"Did not catch any lowercases \n";
	return 0;
}

bool checking (const char *ar)
{
	for (int x = 0; x < strlen(ar); x++)
	{
		if ( islower(ar[x]) )
			return true;
	}
	return false;
}


Our pleasure, but couldn't help just making another small additional simplification. :)
Topic archived. No new replies allowed.