Do While loop ignores statements after it in this function

This function is part of my assignment and it allows a user to change an old password that gets passed by reference into this function. This do while loop will keep asking the user to enter a new password until the condition is met. The condition is that the first half of the old password and the first half of the new password cannot be the same, and the second half of the old password and the second half of the new password cannot be the same. For example, if the old password is ABCD5678 and the new password is ixyz5678, the do while loop will show the message that it doesn't meet the requirements and keep looping to ask a new password until it passes the condition. Once the new password passes the condition, the new password overwrites the old password so this means the old password gets changed into the new password. The problem is that after the do while loop, the output statement doesn't show up and gets ignored when I run the program. For some reason everything after the do while loop gets ignored. This prevents me from changing the old password into the new password. This must be why in the main function, when I try to display the new password, the old password still gets displayed. I'm not sure how to fix this. I have a feeling it is my do while loop that is causing these problems.

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
  bool changePassword(string& password)
{
    string newPW;
    int sizeN;
    int half;
    int lengtholdPW;
    int lengthnewPW;



    do
    {
        cout << "Enter new password, this will keep looping until your password   passes the condition: ";
        getline(cin, newPW);

      


        lengthnewPW = newPW.length();
        lengtholdPW = password.length();
        if (lengthnewPW < lengtholdPW)
        {
            sizeN = lengthnewPW;
        }
        else if (lengtholdPW < lengthnewPW)
        {
            sizeN = lengtholdPW;
        }
        else
            sizeN = lengtholdPW;

        half = sizeN / 2;

        if( ( password.size() <= 2 ) ||
            ( ( password.substr( 0, half ) != newPW.substr( 0, half ) ) &&
              ( password.substr( password.size() - half ) != newPW.substr( newPW.size() - half ) ) ) )

            return true;
              {
                cout << "The new password cannot start or end with " << half
                     << " or more characters that are the same as the old password" << endl << endl;

              }


    } while (!( password.size() <= 2 ) ||
            !( ( password.substr( 0, half ) != newPW.substr( 0, half ) ) &&
              ( password.substr( password.size() - half ) != newPW.substr( newPW.size() - half ) ) ) );
        
          password = newPW;   // everything here and after gets ignored for some reason
          cout << "Your old password is changed and it is now " << password << endl;
          return true;

}
Last edited on
Your problem is with your if condition on line 34.
For one, if your old password is greater than 2 the entire condition will always be true and it will exit the function at line 38 every time.
And lines 35 and 36, I don't think they do what you think they do.
It's saying if the first half of the old one doesn't equal the first half of the new one
and the last half of the old one doesn't equal the last half of the new one, which I don't believe is what you are trying to say based on your cout on line 40.
Also the return true on line 38 is problematic. You probably never want to exit your function there, and that's what it does.
Try instead to set a boolean variable equal to your password checking code, and then put that in an if statement and you can also use it as your do while condition. Like so:

1
2
3
4
5
6
7
8
9
10
11
do {
   //ask for the new password and all the code through line 32

   bool accepted = //your if condition on line 34 after you rewrite it;

   if (!accepted) {
      cout << "The new password cannot start or end with " << half 
             << " or more characters that are the same as the old password"
             <<endl << endl;
   }
} while (!accepted);
Last edited on
Topic archived. No new replies allowed.