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.