2 Replies - 349 Views - Last Post: 06 February 2012 - 08:00 PM Rate Topic: -----

Topic Sponsor:

#1 Vls610  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 29-March 10

Password question

Posted 24 January 2012 - 08:05 PM

Hello,
I'm writting a small password program where the user has to enter a password and the password is check, the program should allow the user three attempts to enter the password if it does not meet the criteria, after the three attempts the program is suppose to terminate.
The problem I'm having is figuring out how to allow the user to enter the password three times.
Any suggestions or pointers in the right direction would be helpful, below is the code I've written so far.
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std; 
 
int const MAXNAME = 20; 
int const MAX = 30;

bool compareName (char [], char []);
bool lengthOf ( char password []) ;
bool lowerAlpha (char password []);
bool upperCase (char password []);
bool lowerCase (char password []);
bool digitCheck (char password[]);
bool nonAlpha (char password[]);
bool nonSpace (char password[]);
bool checkAll (char username [], char password[]);


int main ()
    {
    
        char username [MAXNAME];
        char password [MAX];
        
      cout << " Please enter your user name: ";
         cin.getline (username, MAXNAME);
         
     checkAll ( username, password);
  

         system ("Pause");
       return 0; 
       
   }
   
bool checkAll (char username [], char password[])
   {
                
       
       cout << "\n Please enter your password: ";
         cin.getline (password,MAX); 
 
       compareName (username,password);
       lengthOf (password);
       lowerAlpha(password);
       upperCase (password);
       lowerCase (password);
       digitCheck(password);
       nonAlpha (password);
       nonSpace (password);
           
   }
/*
Purpose: To compare cstring names 
PreCondition: User enters name, and password  
PostCondition: return a bool value 
*/
bool compareName (char username [], char password[])
   {
                 cout << " here " << endl;
     if (strcmp (username,password) == 0)
      {    // 0 in this case means true.
        cout << " - Password and user name are the same" << endl;
        return false; 
      }
   }

 /*
 Purpose: To see if the password contains between 8-12 characters 
 PreCondition: User has entered password 
 PostCondition: return a bool value 
 */ 
bool lengthOf ( char password []) 
   {
    if ( strlen(password) <8 || strlen (password) > 12) 
       cout << " - Password must be between 8-12 characters long" << endl;
    return false;   
   } 

/*
Purpose: To see if password contains either an uppercase or a lowercase alphabetic letter
PreCondition:  User has entered password 
PostCondition: Return a bool value 
*/
bool lowerAlpha (char password [])
   {
    bool compare1, 
         compare2;
    for (int i = 0; i < strlen (password); i++)
     {
        if (isalpha (password [i]))
          return true;
     }
     cout << " - Password must contain alphanumeric character" << endl;
     return false;
     
   }  

/*
Purpose: To see if password is upper case 
Post-Condition: User has entered password
Pre-Condition: return a bool value
*/
bool upperCase (char password [])
   {
    for (int i = 0; i < strlen (password); i++)
     {
       if (isupper (password [i]))
        return true; 
     }  
     cout << " - Password must contain an upper case letter" << endl;
     return false; 
   }
/*
Purpose: To see if password is lowercase !!!
pre-Condition: User has entered password
post-Condition: return a bool value
*/
bool lowerCase (char password [])
   {
    for (int i = 0; i < strlen (password); i++)
     {
       if (islower (password [i]))
        return true; 
     }  
     cout << " - Password must contain an lower case letter" << endl;
     return false;
   }
/*
Purpose: To see if password has a digit 
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool digitCheck (char password[])
   {
     int digit = 0; 
     
     for (int i = 0; i < strlen (password); i++)
       {
        if (isdigit (password[i]))
         {
            digit = atoi (password);
            return true; 
         } 
       }
       cout << " - Password must contain a number" << endl;
       return false;
   }
/*
Purpose: To see if password has nonalpha numeric character
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool nonAlpha (char password[])
   {
  
     for (int i = 0; i < strlen (password); i++)
       {
        if (!isalnum (password[i]))
         {
            return true; 
         } 
       }
       cout << " - Password must contain a non-alphanumeric character" << endl;
       return false;
   }
/*
Purpose: To see if password has a white space
pre-Conditon:user has entered a password
Post-Condition: return a bool value
*/
bool nonSpace (char password[])
   {

     for (int i = 0; i < strlen (password); i++)
       {
        if (isspace (password[i]))
         {
           cout << " - Password must contain no white space" << endl;
           return false;
         } 
       }
       return true;
   }




Is This A Good Question/Topic? 0
  • +

Replies To: Password question

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 438
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Password question

Posted 24 January 2012 - 08:41 PM

All your check functions return a true when the password checks out and false otherwise. The checkall function should do the same. It would be a combination of all the individual checks, something along the lines of

bool checkAll (char username [], char password[])
   {
       ...
       return (compareName (username,password)
               && lengthOf (password)
               && lowerAlpha(password)
               && upperCase (password)
               && lowerCase (password)
               && digitCheck(password)
               && nonAlpha (password)
               && nonSpace (password));         
   }



Then in your main function, you should write a loop to check that keeps running until the password checks out or the total number of attempts > 3. It could look something like this:

int attempt = 0;
bool successful = false;
do {
    successful = checkAll(username, password);
    attempt++;
} while (attempt < 3 && !successful);


This post has been edited by Karel-Lodewijk: 24 January 2012 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 WhiteDog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-January 12

Re: Password question

Posted 06 February 2012 - 08:00 PM

Once i created a program that receive a string and checked again to sure if the people wrote the same thing (for hangman game).

But to write this or password is not good beacause appeared the word in the screen...

'HEY HEY DONT'T LOOK, i will write my password :D '

So, i realized that if i use getch(), the program gets the first thing that i typed.
Basically, you type, quickly the screen is cleaned and appear the '*' carachter in the place of the character you wrote.

Check it out:
        cout<<"Write the password : ";
      for(i=0 ; i<MAXSIZE; i++)
      {
        word[i]=getch(); //receive one letter by time

        system("cls"); //clean screen, to appear the '*'

        if(word[i]==' ') //guarantee that ' ' is a character accepted
         word[i]=' ';
        else
        if(isalnum(word[i])==0) //if you tipy enter, will finish
        {
        word[i]='\0';
        break;
        }
        
        //after clear screen, will put the right numbers of '*'
        cout<<"Write the password : ";
        for(j=0 ; j<i+1 ; j++)
        cout<<"*";
      }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1