Hello,
I am trying to write a program that takes in a password and test it to make sure that it is at least 7 characters long and contains a digit 0-9 or a dollar sign. I can not seem to get it to work, i think there may be a few problems or maybe I am missing something. Any help is greatly appreciated, thank you! Below is my source code.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string password_var;
int i=1;
bool validPass;
int Length_Of;
while (1)
{
cout << "Please enter a password.\n";
cin >> password_var;
validPass = false;
Length_Of = password_var.size();
if (Length_Of >= 7)
{
cout << "Passwords must be at least 7 characters long.\n";
}
while (1)
{
if (password_var[i] >= '0'&&password_var[i] <= '9') || (password_var[i] = '$')
{
validPass = true;
}
else
{
i = i + 1;
}
if (validPass = true || i > Length_Of)break;
}
if (validPass == true)
{
cout << "Thank you, that is a valid password.\n";
else
{
cout << "Passwords must include a digit or a dollar sign(0-9,$).\n";
}
if (validPass == true)break;
}
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string password_var;
bool validPass;
int Length_Of;
while (1)
{
cout << "Please enter a password.\n";
cin >> password_var;
validPass = false;
Length_Of = password_var.size();
if (Length_Of < 7)
{
cout << "Passwords must be at least 7 characters long.\n";
continue; // you don't need to go further in the loop.
}
// initialize i here.
int i = 0;
while (1)
{
// beware of assignment '=' and comparison '=='
if ( (password_var[i] >= '0'&&password_var[i] <= '9')
|| (password_var[i] == '$')
)
{
validPass = true;
}
else
{
i = i + 1;
}
// beware of assignment '=' and comparison '=='
if (validPass == true || i >= Length_Of)
break;
}
if (validPass == true)
{
cout << "Thank you, that is a valid password.\n";
} // add the closing brace.
else
{
cout << "Passwords must include a digit or a dollar sign(0-9,$).\n";
}
if (validPass == true)
break;
}
return 0;
}
in my opinion regexs are for parsing large amounts of data. mine is also easier or as easy to implement. finally, unless it was fixed, <regex> isnt fully supported by all compilers yet (namely gcc) so you would have to use a third party one. but let me repeat for op's sake: you should still learn regexs. they are a very valuable tool