Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help With A Password Verifier

Help With A Password Verifier

Scheduled Pinned Locked Moved C / C++ / MFC
c++businesshelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LilKoopa
    wrote on last edited by
    #1

    Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help

    // LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }

    _ enhzflepE 2 Replies Last reply
    0
    • L LilKoopa

      Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help

      // LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      LilKoopa wrote:

      while (length != 10)

      Your code would only work if the length of the "pass" string is returned as 10. You need to check the condition in the while loop first

      LilKoopa wrote:

      cout << length <<endl;

      This line, above the while loop would be printing 32 ie the size of pass variable since the length would always be greater than 10

      LilKoopa wrote:

      so that I can check if the program actually works

      I hope that you can now check the program after modifying the condition for the while loop

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • L LilKoopa

        Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help

        // LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #3

        sizeof(pass) always returns 4 - it's returning the size in bytes of the pointer that points to the string, not the actual length of the string itself.. if you change string pass; to char pass[100]; and all occurances of sizeof(pass) to strlen(pass) You'll get code that correctly counts the number of letters in the password. The logic in your code then gets a bit funky, and I can't quite tell what you're trying to do there. Often things become much more clear when you separate out the individual parts of a problem into their own functions. Might I suggest a different approach to this problem?

        #include <iostream>
        using namespace std;

        int countNumChars(char *str)
        {
        //
        // TODO: add code for this function
        //
        //
        }

        int countLowerChars(char *str)
        {
        //
        // TODO: add code for this function
        //
        //
        }

        int countUpperChars(char *str)
        {
        //
        // TODO: add code for this function
        //
        //
        }

        bool isValidPassword(char *password)
        {
        int len, numberChars, lowerCaseChars, upperCaseChars;
        bool result = false;
        len = strlen(password);
        numberChars = countNumChars(password);
        lowerCaseChars = countLowerChars(password);
        upperCaseChars = countUpperChars(password);
        result = ((len==10)&&(lowerCaseChars>=1)&&(upperCaseChars>=2)&&(numberChars>=1));
        return result;
        }

        int main()
        {
        char pass[100];

        cout << "Please enter a 10 character password.\\n";
        cout << "You must make sure your password has at\\n";
        cout << "least two uppercase and at least one\\n";
        cout << "lowercase letter and at least 1 number.\\n";
        
        cin >> pass;
        
        while (!isValidPassword(pass))
        {
            cout << "Invalid Password. Please type again.\\n";
            cin >> pass;
        }
        
        cout << "Your password: " << pass << ", is good and accepted.\\n";
        system("pause");
        
        return 0;
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups