dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: C++ Password verification problem.

  1. #1
    Join Date
    Jan 2018
    Posts
    7

    C++ Password verification problem.

    Hello everyone,

    Please advise whats missing or wrong in below code. The assignment states that I'm to accept a password from a user that is a minimum of 6 characters long contains at least 1 upper case letter, 1 lower case letter and 1 number. Below is a screenshot of how the executed code is supposed to look.

    Name:  Capture.PNG
Views: 110
Size:  30.8 KB

    Here is the C++ code. I keep getting error : warning C4715: 'verifyupper', 'verifylower', 'verifyNum' : not all control paths return a value.
    Any help will be greatly appreciated. I'm still very new at this.

    Code:
    #include "stdafx.h"
    #include <cctype> 
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    bool verifypassword(char*);
    int verifyupper(char *);
    int verifylower(char *);
    int verifyNum(char *);
    int verifyAlpha(char *);
    
    int main ()
    {
        const int size = 100;
        char password [size];
    
        std::cout <<"The password must have: \n";
        std::cout <<"        at least 6 characters, \n";
        std::cout <<"        at least one uppercase character, \n";
        std::cout <<"        at least one lowercase character, \n";
        std::cout <<"        at least one numeric digit. \n";
    
        std::cout <<"Enter a password:  \n";
        cin.getline(password, size);
    	
        int length;
        length = strlen(password);
    
        if (length >= 6)
        {	 
            while (!verifypassword (password))
            {
                std::cout << "The password is not valid. Enter password again. \n";
                cin.getline(password, size);
            }
    
            std::cout << "The password is valid.";
        }
        else
        {
            std::cout << "The password must have at least 6 or more characters long\n";
            std::cout << "Please enter  password : "; 
            cin.getline(password, size);
        }
    
        system("pause");
        return 0;
    }
    
    bool verifypassword(char *str)
    { 
        int length = strlen(str);
    
        return verifyupper(str) && verifylower(str) && verifyNum(str) && verifyAlpha(str);
    }
    
    int verifyupper(char *str)
    {
        int num = 0;
        int length = strlen(str);
    
        for (int count = 0; count < length; count ++)
        {
            if (!isupper(str[count]))
                num++;
            else
            {
                std::cout<< "The password must have at least one upper case character. \n";
                return num;
            }
        }
    }
    
    int verifylower(char *str)
    {
        int num = 0;
        int length = strlen(str);	
    
        for (int count = 0; count < length; count ++)
        {
            if (islower(str[count]))
                return true;
            else
            {
                std::cout<< "The password must have at least one lower case character. \n";
                return false;
            }
        }
    }
    
    int verifyNum(char *str)
    {
        int num = 0;
        int length = strlen(str);	
    
        for (int count = 0; count < length; count ++)
        {
            if (isdigit(str[count]))
                return true;
            else
            {
                std::cout<< "The password must have at least one digit case character. \n";
                return false;
            }
        }
    }
    Last edited by 2kaud; January 21st, 2018 at 06:04 AM. Reason: Added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,749

    Re: C++ Password verification problem.

    It might help if you indented your code and posted it in [code][/code] bbcode tags, e.g.,
    Code:
    int verifyupper(char *str)
    {
        int num = 0;
        int length = strlen(str);
        for (int count = 0; count < length; count ++)
        {
            if (!isupper(str[count]))
                num++;
            else
            {
                std::cout<< "The password must have at least one upper case character. \n";
                return num;
            }
        }
    }
    Now, we can see that you have a logic error: if str[0] is not an uppercase letter, you print an error message and return 0. But if str[0] is an uppercase letter, that means that you print an error message saying that "The password must have at least one upper case character", which it would have! Rather, your logic should be along these lines:
    Code:
    for each character in the string
        if the character is an uppercase letter
            return true
    return false
    Of course, this logic error is not in itself the reason for the "not all control paths return a value" compile warning. This warning is because in your code for verifyupper (and the other functions named), it is possible to trace the flow of control and reach the end without returning a value, despite the function being declared as returning an int (it should probably return a bool instead, unless you want to consistently use int to mean bool given that this was historically done in C... but you're inconsistent with verifypassword). For example, suppose that the string is empty. Then control does not enter the loop since count < length evaluates to 0 < 0, which evaluates to 0 (i.e., false). Hence, control goes to the end of the function... and finds there's no return statement! Fixing the logic according to the pseudocode I mentioned would also fix this.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,362

    Re: C++ Password verification problem.

    A couple of further observations.

    Why are c-style null-terminated char arrays used in c++ code rather than c++ string?

    If the length of the entered password is less than 6, an error message is displayed and a new password requested. However this new password is not checked.

    There is no verifyAlpha() function body.

    There is no need for verifypassword() - and no need for variable length in that function as it is not used. The main() while loop could be coded as

    Code:
    while (!(verifyupper(str) && verifylower(str) && verifyNum(str) && verifyAlpha(str))) {
    ...
    }
    It is preferable to get into the habit now of using pre-increment (++count) rather than post-increment (count ++) where possible. When used with an integer (as here) it doesn't really matter but later on when you cover more advanced c++ concepts it does - and a habit is hard to break!

    When the contents of a variable doesn't change then it should be defined a const. eg

    Code:
    const int length = strlen(str);
    Likewise, when passing a pointer (char *) and the data to which the pointer references doesn't change then it is better to pass as a const

    Code:
    int verifyupper(const char *str)
    It is preferable to have a function perform just one thing. So verifyupper() just verifys that one char is upper case and return true or false accordingly. However the verification routines display an error message if verification fails. What if later this routine is required to be used with a gui interface? In this case displaying a message on the console is not appropriate. The calling function should handle what to do whether verification passes or fails.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.1)

  4. #4
    Join Date
    Jan 2018
    Posts
    7

    Re: C++ Password verification problem.

    Thank you laserlight & 2kaud.

    I really do appreciate taking your time looking at the code for me. I took into consideration all your suggestions, cleaned it up and the coding seems to make more sense now.
    Dealing with 1 more error; (Run-Time Check Failure #3 - The variable 'str' is being used without being initialized). Will try figure it out.

    Thanks again guys.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,362

    Re: C++ Password verification problem.

    If you post your current code, we'll take a look.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.1)

  6. #6
    Join Date
    Jan 2018
    Posts
    7

    Re: C++ Password verification problem.

    Thank you 2kaud.

    I'm still very new at this, below is the current code.

    Code:
    #include "stdafx.h"
    #include <cctype> 
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    bool verifyupper(const char *str);
    bool verifylower(const char *str);
    bool verifyNum(const char *str);
    
    int main ()
    {
    	const int size = 100;
    	char password [size];
    	const char *str;
    
    	std::cout <<"The password must have: \n";
    	std::cout <<"        at least 6 characters, \n";
    	std::cout <<"        at least one uppercase character, \n";
    	std::cout <<"        at least one lowercase character, \n";
            std::cout <<"        at least one numeric digit. \n";
    
    	std::cout <<"Enter a password:  \n";
    	cin.getline(password, size);
    	
    	if (size >= 6)
    	{	 
               while (!(verifyupper(str) && verifylower(str) && verifyNum(str)))
    	   {
    	        std::cout << "The password is not valid. Enter password again. \n";
    	        cin.getline(password, size);
    	   }
    	   
    	   std::cout << "The password is valid.";
    	   return 0;
        }
        else
        {
    	std::cout << "The password must have at least 6 or more characters long\n";
    	std::cout << "Please enter  password : "; 
            cin.getline(password, size);
        }
    
        system("pause");
        return 0;
    }
    
    
    bool verifyupper(const char *str)
    {
    	const int length = strlen(str);	
    	
    	for (int count = 0; count < length; count ++)
            {
                if (isupper(str[count]))
    			return true;
    	    else
    	    {
    	        std::cout<< "The password must have at least one upper case character. \n";
    	        return false;
                }
    	}
    }
    
    bool verifylower(const char *str)
    {
    	const int length = strlen(str);	
    	
    	for (int count = 0; count < length; count ++)
            {
                if (islower(str[count]))
                    return true;
                else
    	    {
    	        std::cout<< "The password must have at least one lower case character. \n";
    		return false;
                }
            }
    }
    	
    bool verifyNum(const char *str)
    {
    	const int length = strlen(str);	
    	
    	for (int count = 0; count < length; count ++)
            {
                if (isdigit(str[count]))
                    return true;
                else
                {
    	         std::cout<< "The password must have at least one digit case character. \n";
    	         return false;
                }
            }
    }
    Last edited by 2kaud; January 21st, 2018 at 02:09 PM. Reason: Added code tags

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,749

    Re: C++ Password verification problem.

    Please post your code within [code][/code] bbcode tags.

    As for the compile warning: it is as it says, you used str (e.g., by calling verifyupper(str)) without causing it to have an initial value. It seems that you may be a bit confused as to whether to use password or to use str, so once you decide on that you should be able to fix this problem.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,362

    Re: C++ Password verification problem.

    When posting code, you need to use code tags so that the code is readable. Either surround your formatted code with the tags or Go Advanced, select the formatted code and click '#'.

    As laserlight mentions in post #7, in main() you don't need variable str. The verifyxxxx() functions now take password as an argument as they are now used in main() and not in verifypassword().

    Note that you still have logic errors as highlighted by laserlight in post #2 - which you'll find once you get the code to compile.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.8.1)

  9. #9
    Join Date
    Jan 2018
    Posts
    7

    Re: C++ Password verification problem.

    Thank you again guys.
    Now making positive progress. So much eye opening in all of your suggestions. I was feeling lost but now getting on track.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,518

    Re: C++ Password verification problem.

    Quote Originally Posted by Jt37 View Post
    Thank you again guys.
    Now making positive progress. So much eye opening in all of your suggestions. I was feeling lost but now getting on track.
    Your code is way more complicated than it needs to be.

    You could do it all in one loop over the password. Consider something like

    Code:
    bool bHasUpper = false;
    bool bHasLower = false;
    bool bHasNumber = false;
    
    for (int count = 0; count < length; count ++)
    {
          if (isupper(str[count]))
              bHasUpper = true;
         else
    ...
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)