-2

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.

Question

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; 
              ^
  • Voting to close as a typo. Move the cout statements you have before `void printStrongpass(string& input)` to be inside `main` before you get the password. – NathanOliver May 25 '21 at 21:36
  • 1
    That's simply not how c++ works. You cannot execute any statements outside a function body. – πάντα ῥεῖ May 25 '21 at 21:37
  • 2
    Probably unrelated: Please read [Why should I **not** `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo May 25 '21 at 21:39
  • @NathanOliver I tried moving the aforementioned into int main and got different set of errors shown above –  May 25 '21 at 21:53

1 Answers1

1

Try this:


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

void start_msg() {
    //get the password from the user
    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 ' ' (space), tab, newline, and return," << 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;
}

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()
{
    start_msg();
    std::string password;
    cin >> password;
    printStrongpass(password);
    return 0;
}

And as @TedLyngmo said:

Probably unrelated: Please read Why should I not #include <bits/stdc++.h>?? and Why is "using namespace std;" considered bad practice? considered bad practice?

Firstly, you cannot have function calls, etc, outside of functions, which is why I added start_msg().

Secondly, make sure to close your strings correctly.

  • Note that the parameter of the functions `islower`, `isupper` and `isdigit` is of type `int`, not `char`. According to the [documentation of these functions](https://en.cppreference.com/w/cpp/string/byte/isdigit), calling any of these functions with a negative value other than `EOF` will cause undefined behavior according to the ISO C++ standard. On most platforms, character codes above `127` are stored as negative numbers in variables of type `char`. Therefore, you should probably cast `input[i]` to `unsigned char` before passing the value to any of these functions. – Andreas Wenzel May 25 '21 at 23:15
  • For the historical reasons for the rather strange behavior described in my previous comment, see [this answer](https://stackoverflow.com/a/45007070/12149471) to another question. – Andreas Wenzel May 25 '21 at 23:16