Search code examples
c++chars

How to check what return functions and correctly calculate chars


Welcome. I have two problems. Firstly - function size bool (const char * pass) to check whether the amount of the chars inside of string is at least 8, but there is something wrong. Its always showing that there is minimum 8 chars, even is string contain only 3 chars.

My job is to create several small functions for checking the correctness of the entered string of chars. Can You help you with this one? If all small functions inside of bool check(...) return true i need to write in the console "STRING IS OKAY".

I will be grateful for any suggestions.

    #include <iostream>
    #include <cctype>     
    using namespace std;

    //Check the amount of chars
    bool size (const char* pass){
        if(sizeof(pass) > 7)
                        return true;
    }

    //Checks if the ASCII are located between 32 to 126
    bool isPrint (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isprint(pass[x]))
                return true;
        }
    }


    //Check the amount of numbers
    bool isNum (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isdigit(pass[x]))
                return true;
        }
    }

    //Check the amount of Upper letters
    bool isUpperLetter (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(isupper(pass[x]))
                return true;
        }       
    }

    //Check the amount of lower letters
    bool isLowerLetter (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(islower(pass[x]))
                return true;
        }       
    }

    //Check the amount of Punctuation Marks
    bool isPunctMark (const char* pass){
        for(int x=0; x <= sizeof(pass); x++){
            if(ispunct(pass[x])){
                return true;
            }
        }       
    }


    //All small moduls together
    bool check (const char* pass){
        size(pass);
        isPrint(pass);
        isNum(pass);
        isUpperLetter(pass);
        isLowerLetter(pass);
        isPunctMark(pass);
    }

    int main() {
        char x;
        cout << "Enter the string of characters" << endl;
        cin >> x;
        const char *password = &x;
        check(password);
    }

Solution

  • sizeof(pass) returns the size of the pointer. Which is implementation specific, and if your function is always returning true, we can guess that sizeof(char*) is 8, implying you have a 64-bit system.

    On many other systems, it will return 4, or may even return 2 or 1, dependant on the architecture.

    You probably want to check the length of the string the pointer is pointing to like so:

    int len=strlen(pass);
    if(len>=8)   //Check for >=8 is clearer than >7)
    {
       return true;
    }
    

    You could also iterate over the string and check for null. But why bother when there is a nice std library routine that does the job.

    to run all checks, do something like

    bool isValid(const char* const pass)
    {
         if(!isPrint(pass))
         {
    
             return false;
         }
         if (!isNum(pass))
         {
             return false;
          }
         //etc
     }
    

    You could also have a big long

    if(isPrint(pass)&&isNum(pass) .....)
    {
        return true;
    }
    

    but that would be messier, and harder to debug.