password restrictions homework

I am trying to figure out why the website that i have to submit my homework in won't accept this code below as an answer. Redundancy aside, the program seems to work exactly the way it supposed to but i keep getting an error message that says "Checking output Your standard output is not what was expected."

the assignment goes as follows:
"- A password must have at least eight characters.
- A password must consist of only letters and digits.
- A password must contain at least two digits.

Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise."

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
using namespace std;

int main(){
    
    string password;
    int count = 0;
    int specials = 0;
    bool valid_password = true;

    cout << "- A password must have at least eight characters. "<< endl;
    cout << "- A password must consist of only letters and digits." << endl;
    cout << "- A password must contain at least two digits." << endl;

    
    do{
    cout << "Please enter a password: ";
    //getline(cin, password,'\n');
    cin >> password;

    valid_password = true;
    count = 0;
    specials = 0;
    
    for(int i = 0; i < password.length(); i++)
    {
      if ( !isalnum(password[i]))
      {
        specials++;
        valid_password = false;
      }
      else{
        valid_password = true;
      }
    }
    if((password.length() < 8) || (specials != 0)){
        cout << "Invalid password" << endl;
        valid_password = false;
      }
    
    for(int j = 0; j < password.length(); j++){
      if(isdigit(password[j]))
      {
        count++;
      }
    }

    if(count < 2 || (password.length() < 8)){
        valid_password = false;
    }
    
    if(valid_password){
      cout << "Valid Password" << endl;
    }
    }while(valid_password == false);
    
    return 0;
}
Are you supposed to prompt for multiple passwords in a loop? The assignment doesn't ask for that.

Sometimes you don't indicate anything:
$ ./foo
- A password must have at least eight characters.
- A password must consist of only letters and digits.
- A password must contain at least two digits.
Please enter a password: foo
Invalid password
Please enter a password: abcdefghij1
Please enter a password: abcdefghij1
Please enter a password: foo
Invalid password

I agree that it appears to work.

the letter of the law approach:
Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise."


that does not say to write 3 lines of unnecessary text first. Maybe that is breaking the program checker.
I was wondering if i had to loop it or not but this assignment is in the looping section of the book so that's why I went with a loop.

I also tried the following code (without the loop) but it showed me the same message error:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;

int main(){
    
    string password;
    int count = 0;
    int specials = 0;
    bool valid_password = true;

    cout << "- A password must have at least eight characters. "<< endl;
    cout << "- A password must consist of only letters and digits." << endl;
    cout << "- A password must contain at least two digits." << endl;

    
    cout << "Please enter a password: ";
    cin >> password;
    
    for(int i = 0; i < password.length(); i++)
    {
      if ( !isalnum(password[i]))
      {
        specials++;
      }
    }
    
    for(int j = 0; j < password.length(); j++){
      if(isdigit(password[j]))
      {
        count++;
      }
    }

    if(count < 2 || (password.length() < 8) || (specials != 0)){
        valid_password = false;
    }
    
    if(valid_password){
      cout << "Valid Password" << endl;
    }
    else
      cout << "Invalid password" << endl;
    
    
    return 0;
}


I also removed the lines of text in the beginning like you mentioned but it didn't work.
Last edited on
Please enter a password: MyPass123
Valid Password

Sounds like the automated grading system is expecting output to be in a way different from what you're providing. Perhaps it's case sensitive? e.g. "invalid password".
Last edited on
wow, you were exactly right. The grading system was case sensitive. I got it to work. Thank you all for your help I sincerely appreciate it. I'm new to programming so seeing the support from this community motivates me to keep on learning.
Registered users can post here. Sign in or register to post.