Sorry, validation and arrays again!

This is a discussion on Sorry, validation and arrays again! within the C++ Programming forums, part of the General Programming Boards category; Please, just a little more explaining here.......I got a really good answer when I posted this last time, but I ...

  1. #1
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158

    Question Sorry, validation and arrays again!

    Please, just a little more explaining here.......I got a really good answer when I posted this last time, but I cant figure it out!!! Im new to this ok, and although I learn fast its this validation I JUST CANT seem to understand.My code below works fine IF I input a 6 digit password, if not it says 'invalid' (as it should) but then skips a level upwards. Something to do with whats left in keyboard buffer from cin.getline? I dunno. Debugger is no help (it seems). It was written on Borland 5 proff. I will really appreciate some festive spirit here !! Thanx in advance...


    ////////////////////////////////////////////////////////////////////////////////
    /// Program to initiate a password system/////////
    //Allow administrator to input a 6 character password then any
    //USER can try to login. The user will be told either invalid OR
    //the LEVEL of access they have.i.e level of access will equal
    // INT i.......
    ////////////////////////////////////////////////////////////////////////////////

    #include <iostream.h>
    #include <iomanip.h>
    #include <conio.h>
    #include<string.h>

    const int MAX_PASSWORDS =10;
    const int CHAR_LIMIT=7;

    void main ()
    {
    char Password[MAX_PASSWORDS][CHAR_LIMIT];
    char User_Password;


    for (int i=0; i<=MAX_PASSWORDS; i++)
    {
    cout << "Please enter password for level " << i+1<< " access ";
    cin.getline(Password[i],CHAR_LIMIT);

    if(strlen(Password[i])!= CHAR_LIMIT-1)
    {
    cout << "Invalid length";
    cin.getline(Password[i],CHAR_LIMIT);
    }
    }

    }


    Seriously though...I have tried all sorts of combinations of brackets on the strlen(password[i] line to no avail.....I know I had to remove a getchar I had at the bottom, but why does it skip a level when an invalid password is input???? Oh yes that is after you press enter TWICE.?? WHY?? Many thanx in advance of a training session (which will be mucho appreciated)

  2. #2
    Unregistered
    Guest
    To answer your question:

    Use a loop rather than an if statement when evaluating the length of the password entered. For example:

    while(strlen(Password[i])!= CHAR_LIMIT-1)
    //etc.

    Also it should be:

    i < MAX_PASSWORDS

    not:

    i <= MAX_PASSWORDS

    And I think it should be:

    cin.getline(Password[i], CHAR_LIMIT - 1);

    rather than:

    cin.getline(Password[i],CHAR_LIMIT);

    However I doubt you want to overwrite the passwords stored in the array of passwords so it should be:

    char User_Password[CHAR_LIMIT];

    and:

    cin.getline(User_Password, CHAR_LIMIT - 1);

    But what you really want to find out is not if length of user input is equal to length of stored password but whether the entry is the same as the current password. Therefore strcpy() should do all validation in one statement.

    while(!strcpy(Password[i], User_Password))
    {
    cout << "I am sorry. The password you entered is invalid." << endl;
    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    When an invalid password was entered, the for loop still went around, and i was still increased. You need to use a nested while loop to validate the passwords.
    Code:
    #include <iostream.h> 
    #include <iomanip.h> 
    #include <conio.h> 
    #include<string.h> 
    
    const int MAX_PASSWORDS =10; 
    const int CHAR_LIMIT=7; 
    
    int main () 
    { 
    	char Password[MAX_PASSWORDS][CHAR_LIMIT];
    	char User_Password;
    	char input[255]; //use a temp buffer for input
    
    	for (int i=0; i<MAX_PASSWORDS; i++) 
    	{ 
    		cout << "Please enter password for level " << i+1<< " access "; 
    		cin.getline(input,254); // get the input
     
                          //if the lenght is not right, tell the user and get a new one
    		while( strlen(input) != (CHAR_LIMIT-1) ) 
    		{ 
    			cout << "Invalid length"; 
    			cout << "Please enter password for level " << i+1<< " access "; 
    			cin.getline(input,254); 
    		} 
                                    //copy the correct input to the password array
    		strcpy(Password[i],input);
    	} 
    
    	return 0;
    }
    The problem with your code was that you were using an if statement to check for an invalid length, the for loop would start over with i increased by one.
    This code i posted will allow to check for passwords that are to short or to long, and keep prompting the user for a correct one.
    The code that you and Unregistered posted would only input the first CHAR_LIMIT-1 chars using get line, so if the user inputted a longer password it would truncate the end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Arrays question
    By taugust7 in forum C++ Programming
    Replies: 36
    Last Post: 04-14-2009, 01:29 PM
  2. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  3. arrays and validation (basic stuff)
    By Fountain in forum C++ Programming
    Replies: 2
    Last Post: 12-21-2001, 04:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21