Stuck on a program. Help please.

I am new to C++ and am stuck on a program. I've got to create a password check program that makes sure the password rules are followed. Below is the code and the rules i have typed in the comments at the beginning of the program. I have to use loops and cant use arrays for this. Thanks in advance.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
//Prompt user to entered a password to be tested
cout << "Password must be at least 8 characters long." << endl;
cout << "Must contain letters and numbers only." << endl;
cout << "And contain a least two numbers." << endl;
cout << "Enter a password: ";
string password;
getline(cin, password);

int low = 0;
//Index of last character entered
int high = password.length() - 1;
int length = password.length();
int digitCount = 0;
bool isValid = false;

while (length >= 8)
{
while (low <= high)
{
if (isalpha(low))
{
isValid = true;
}
else if (isdigit(low))
{
isValid = true;
digitCount++;
}
else
{
isValid = false;
}

low++;
}

if (digitCount < 2)
isValid = false;
}

if (isValid == true)
cout << "Valid Password" << endl;
else if (isValid == false)
cout << "Invalid Password" << endl;

system("Pause");
return 0;
}
if (length < 8) {
std::cout << "input illegal" << std::endl;
return 0;
}

unsigned int unAlphaCounter = 0, unDigitCounter = 0;
for (size_t szIndex=0; szIndex<length; ++szIndex) {
if (isalpha(password[szIndex]) {
unAlphaCounter++;
continue;
}
if(isdigit(password[szIndex]) {
unDigitCounter++;
continue;
}
std::cout << "input illegal" << std::endl;
break;
}
return 0;
}
Thank you so much. I figured out what i was doing wrong in mine by your example. Works perfect now.
Last edited on
Topic archived. No new replies allowed.