Help with password requirements code.
Mar 27, 2022 at 3:58am UTC
I have been doing wicked good lately thanks to all the help I've been getting here but I am having some issues with this code. I am trying to write a code that "Output "Valid" if input contains fewer than 6 numbers and input's length is less than or equal to 8. Otherwise, output "Invalid"."
I feel like I need a npos somewhere but I can't figure out where because I am so new.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
using namespace std;
int main() {
bool isValidPasswd;
string keyStr;
cin >> keyStr;
if ((keyStr.length(<=8)&&(keyStr(isdigit(<6)){
isValidPassword = true ;
}
else {
isValidPassword = false ;
}
if (isValidPasswd) {
cout << "Valid" << endl;
}
else {
cout << "Invalid" << endl;
}
return 0;
}
Mar 27, 2022 at 4:11am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <iostream>
using namespace std;
int main()
{
bool isValidPasswd{true };
string keyStr;
cin >> keyStr;
if (keyStr.length() <= 8)
{
isValidPasswd = false ;
}
int count{0};
for (int i = 0; i < keyStr.length(); i ++)
{
if ( std::isdigit(keyStr[i]) )
count++;
}
if ( count < 6 )
isValidPasswd = false ;
if ( isValidPasswd == true )
cout << "Valid" << endl;
else
cout << "Invalid" << endl;
return 0;
}
Mar 27, 2022 at 4:17am UTC
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include <iostream>
using namespace std;
int main()
{
bool isValidPasswd{false };
string keyStr;
cin >> keyStr;
int count{0};
for (int i = 0; i < keyStr.length(); i ++)
{
if ( std::isdigit(keyStr[i]) )
count++;
}
if (keyStr.length() >= 8 and count >= 6)
{
isValidPasswd = true ;
}
if ( isValidPasswd == true )
cout << "Valid" << endl;
else
cout << "Invalid" << endl;
return 0;
}
Mar 27, 2022 at 9:45am UTC
Valid - if input < 6 digits and inputLength <= 8 ?? Seems a queer valid requirement. Just a single 'a' or <space> would satisfy this. Have you got valid/invalid the wrong way round?
Assuming as given, then:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <algorithm>
#include <cctype>
int main() {
std::string keyStr;
std::cin >> keyStr;
const bool isValidPasswd {keyStr.length() <= 8 && std::count_if(keyStr.begin(), keyStr.end(), [](unsigned char ch) {return std::isdigit(ch); }) < 6};
std::cout << (isValidPasswd ? "Valid\n" : "Invalid\n" );
}
Last edited on Mar 27, 2022 at 10:26am UTC
Topic archived. No new replies allowed.