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
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.
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
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]))
returntrue; // jump out of loop as soon as find first lowercase char
}
returnfalse;
}
#include <iostream>
#include <cstring>
usingnamespace 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 = newchar [strlen(buffer)+1]; // ALLOCATE
strcpy(arr,buffer);
//checking (arr); REMOVE POINTLESS CALL
result = checking (arr);
if (result == true)
cout <<"Caught a lowercase \n";
elseif ( 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]))
returntrue; // jump out of loop as soon as find first lowercase char
}
returnfalse;
}
2. You can pass a char array to a function that takes a char* so you don't need the new (and delete) anyway.
#include <iostream>
#include <cstring>
usingnamespace std;
bool checking (constchar *); // 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 (constchar *ar) // safer to use const char*
{
constint len = strlen(ar); // call strlen() just once! (const as doesn't change)
for (int x = 0; x < len; x++)
{
if (islower(ar[x]))
returntrue; // jump out of loop as soon as find first lowercase char
}
returnfalse;
}
#include <iostream>
#include <cstring>
usingnamespace std;
bool checking (constchar *); // 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 (constchar *ar)
{
bool result = false;
for (int x = 0; x < strlen(ar); x++)
{
if (islower(ar[x]))
returntrue;
}
returnfalse;
}
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
returntrue; // char (0 is same as '\0') -- which only exists
// at index strlen(ar), so will be false for first char)
elseif (islower(ar[x] = 0)) // operator= sets element to 0 (or '\0') and
returnfalse; // returns 0 (the value set), so if is valuated
// against 0 which is same as false
}
}