I'm trying to create a password login program in C++. So there is some mistake in looping.
If the entered password satisfied all the conditions means it had to come out of loop but it doesn't end. So if anyone know means explain me properly. I just have started to learn programming.
Look at the output first. It is showing it must include uppercase and digits and I enter some correct password and when I enter the wrong password knowingly, it is showing that it's good password, which is unexpected for me.
Here's what I've attempted to do:
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{
int alp = 0, i, num = 0, j;
char a[10];
do
{
cout << "\nEnter a password:";
cin >> a;
if (strlen(a) > 8)
{
for(i = 0; i <= sizeof(a); i++)
{
if(isupper(a[i]))
alp++;
else if(isdigit(a[i]))
num++;
}
if (alp > 0 && num > 0)
cout << "\nGood password\n";
else
cout << "Your password must include atleast one digit and one uppercase\n";
}
else
cout << "\nYour password must have atleast 8 characters";
} while(true);
return 0;
}
Here's the output:
Enter a password:harry
Your password must have atleast 8 characters
Enter a password:harrypot
Your password must have atleast 8 characters
Enter a password:harrypott
Your password must include atleast one digit and one uppercase
Enter a password:Harry1817t
Good password
Enter a password:harrypott
Good password
Enter a password: // forever
Any help on this is appreciated.