I get an error that says "implicit conversion loses integer precision"
What does this mean?
Also my second problem is that when I test this program it seems to work ok until I have run enough passwords through to turn each of the bools to true and then it will accept the next password I enter regardless of whether or not it actually fits the criteria.
example
PW entry #1: 1111111 (error about no upper case or lower case, but will turn the has_digits flag to true and it will stay that way regardless of subsequent pw entries)
#2: 11111A will set has_upper to true
#3: 11111a will set has_lower to true
#4 123 This password doesn't meet any of the criteria but will be accepted because all the flags have been tripped to true.
How to I reset the flags to false for every iteration of the loop?
//
// main.cpp
// Password Validator
//
// Created by Adam Windsor on 9/3/14.
// Copyright (c) 2014 Adam Windsor. All rights reserved.
//
#include <iostream>
#include <cctype>
#include <cstring>
usingnamespace std;
void validatePassword(char [ ]);
constint SIZE=100;
int main()
{
char password[SIZE];
cout << "Please enter a new password " << endl;
cout << "The password should be at least 6 characters long. " << endl;
cout << "The password should contain at least one uppercase and at least one lowercase letter. " << endl;
cout << "The password should have at least one digit. " << endl;
validatePassword(password);
return 0;
}
void validatePassword(char *password)
{
int length;
bool has_uppers = false, has_lowers = false, has_digits = false;
do
{
cin.getline(password, SIZE);
length=strlen(password);
while (length < 6)
{
cout << "Please enter at least six characters " << endl;
cin.getline(password, SIZE);
length=strlen(password);
}
//int length = strlen(password);
for( int i=0; i<length; i++)
{
char ch = password[i];
has_uppers |= isupper(ch);
has_lowers |= islower(ch);
has_digits |= isdigit(ch);
}
if (has_uppers == false)
{
cout << "Password must contain an upper case letter." << endl;
}
if (has_lowers == false)
{
cout << "Password must contain a lower case letter" << endl;
}
if (has_digits == false)
{
cout << "Password must contain a number" << endl;
}
if (has_uppers == false || has_lowers == false || has_digits == false)
{
cout << "please re-enter password" << endl;
}
} while (has_uppers == false || has_lowers == false || has_digits == false);
cout << password << endl;
}
I get an error that says "implicit conversion loses integer precision"
What does this mean?
It means that you're using the wrong type of variable to hold the return value from strlen(). This function doesn't return an int it returns a size_t so length should be a size_t not an int.
How to I reset the flags to false for every iteration of the loop?
Are you going to use these variables after you are finished with the loop? If not then why not declare them inside the loop body. Keep your variables as local as possible.