I'm currently learning how to program in C++ and one of the practical examples is to write a program allowing user to enter a PW and check if it has a certain type of letter,number. Currently writing in repl.it before pasting to .txt and compiling using makefile. There are no resources or similar examples I could find explaining how to do this, nor have I fully grasped enough to ID the correct keywords to use to search.
Attempt: (edited according to NathanOliver advice)
#include <bits/stdc++.h>
#include <iostream>
#include <cctype>
using namespace std;
//get the password from the user
void printStrongpass(string& input)
{
int n = input.length();
// Check lower alphabet in string
bool hasLower = false, hasUpper = false;
bool hasDigit = false, specialChar = false;
string normalChars = "abcdefghijklmnopqrstu"
"vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
for (int i = 0; i < n; i++) {
if (islower(input[i]))
hasLower = true;
if (isupper(input[i]))
hasUpper = true;
if (isdigit(input[i]))
hasDigit = true;
size_t special = input.find_first_not_of(normalChars);
if (special != string::npos)
specialChar = true;
}
// Password pass?
if (hasLower && hasUpper && hasDigit &&
specialChar && (n >= 8))
cout << "Valid" << endl;
else if ((hasLower || hasUpper) &&
cout << "Invalid" << endl;
specialChar && (n >= 6))
cout << "Invalid" << endl;
else
cout << "Invalid" << endl;
specialChar && (n >= 6))
}
// Driver code
int main()
cout << "Please enter your password.\n";
cout << " Your password must be at least 8 characters long.";
cout << "it has no whitespace characters;<< endl;
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
cout << "at least 1 character is a digit, 0-9,"<< endl;
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
cout << "at least 1 character is one of these four punctuation
characters: !@#$" << endl;
{
cin >> password;
printStrongpass(input);
return 0;
}
Except this gives
main.cpp:7:1: error: unknown type name 'cout'
cout << "Please enter your password.\n";
^
main.cpp:7:6: error: expected unqualified-id
cout << "Please enter your password.\n";
^
main.cpp:8:1: error: unknown type name 'cout'
cout << " Your password must be at least 8 characters long.";
^
main.cpp:8:6: error: expected unqualified-id
cout << " Your password must be at least 8 characters long.";
^
main.cpp:9:1: error: unknown type name 'cout'
cout << "it has no whitespace characters;<< endl;
^
main.cpp:9:6: error: expected unqualified-id
cout << "it has no whitespace characters;<< endl;
^
main.cpp:9:9: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "it has no whitespace characters;<< endl;
^
main.cpp:10:9: warning: missing terminating '"' character [-Winvalid- `pp-token]`
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
^
main.cpp:12:1: error: unknown type name 'cout'
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:12:6: error: expected unqualified-id
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:13:1: error: unknown type name 'cout'
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:13:6: error: expected unqualified-id
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:14:1: error: unknown type name 'cout'
cout << "at least 1 character is one of these four punctuation character...
^
main.cpp:14:6: error: expected unqualified-id
cout << "at least 1 character is one of these four punctuation character...
^
main.cpp:47:25: error: expected ';' after expression
specialChar && (n >= 6))
^
;
main.cpp:47:25: error: expected expression
main.cpp:47:14: warning: expression result unused [-Wunused-value]
specialChar && (n >= 6))
~~~~~~~~~~~ ^ ~~~~~~~~
main.cpp:53:12: error: use of undeclared identifier 'password'
cin >> password;
^
main.cpp:54:21: error: use of undeclared identifier 'input'
printStrongpass(input);
^
What is the correct method to acquire the desired result and where/what are the deficiencies in my attempt that I need to correct?
A weak aspect I cant find resources for is user input as it appears to be less straightforward than Pythons: username = input("Enter username:").
Errors after edit:
main.cpp:7:1: error: unknown type name 'cout'
cout << "Please enter your password.\n";
^
main.cpp:7:6: error: expected unqualified-id
cout << "Please enter your password.\n";
clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:37:24: error: expected ';' after expression
clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:37:24: error: expected ';' after expression
specialChar && (n >= 6))
^
;
main.cpp:37:24: error: expected expression
main.cpp:37:13: warning: expression result unused [-Wunused-value]
specialChar && (n >= 6))
~~~~~~~~~~~ ^ ~~~~~~~~
main.cpp:41:3: error: expected function body after function declarator
cout << "Please enter your password.\n";
^
main.cpp:42:3: error: unknown type name 'cout'
cout << " Your password must be at least 8 characters long.";
^
main.cpp:42:8: error: expected unqualified-id
cout << " Your password must be at least 8 characters long.";
^
main.cpp:43:3: error: unknown type name 'cout'
cout << "it has no whitespace characters;<< endl;
^
main.cpp:43:8: error: expected unqualified-id
cout << "it has no whitespace characters;<< endl;
^
main.cpp:43:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "it has no whitespace characters;<< endl;
^
main.cpp:44:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "whitespace characters are ' ', '\t', '\n', and '\r',<< endl;
^
main.cpp:46:3: error: unknown type name 'cout'
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:46:8: error: expected unqualified-id
cout << "at least 1 character is a lowercase letter, a-z"<< endl;
^
main.cpp:47:3: error: unknown type name 'cout'
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:47:8: error: expected unqualified-id
cout << "at least 1 character is an uppercase letter, A-Z"<< endl;
^
main.cpp:48:3: error: unknown type name 'cout'
cout << "at least 1 character is one of these four punctuation
^
main.cpp:48:8: error: expected unqualified-id
cout << "at least 1 character is one of these four punctuation
^
main.cpp:48:11: warning: missing terminating '"' character [-Winvalid-pp-token]
cout << "at least 1 character is one of these four punctuation
^
main.cpp:49:19: warning: missing terminating '"' character [-Winvalid-pp- token]
characters: !@#$" << endl;
^