Why doesn't this loop work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void check_valid_password()
{
     char string[16];
     bool loop = true;
     
     do{
          loop = true;
          cout << "Enter the password you would like to use,\n"
               << "it must conain atleast 5 letters, start with\n"
               << "a letter, contain atleast one capital letter\n"
               << "and have no punctuation.";
               cin.getline (string,16);
               if (strlen(string) < 5)
                  {
                                  loop = false;
                                  cout << "Your password is too short! Must be atlest 5 characters.";
                  }
        }
          while (loop = false);
     
     
     system("pause");
}
this loop will terminate if you enter a string with less than 5 chars.

I would switch the loop to a while loop instead of a dowhile loop

your bool value Loop is set by your program so in theory the program will always run atleast once even if you have a while loop


Last edited on
What why? I said while loop = false then repeat the whole thing.

And then its less than 5 loop = false? Right?!
DOH... stupid me... you can do it however you want it is part of your own style. I enjoy while loops vs dowhile loops... 1 less line. but they all have uses.

Here is your problem.. you are going to hate me...

while (loop == false);

hah gl have fun

your while statement sets loop equal to false. and thus defeating any logic operators. your loop will go forever if you didnt have user input.
Last edited on
@whitesnow,
Hi,

You are using assignment operator in your loop condition rather then relational and logical operators.

while (loop = false);

so right loop will be

while (loop==false);

and now loop will exist when you will enter more then five char password.

Regards

Duuhhh, I was a bit late answer is already there by amaac.
lol i looked at this code a few times before i saw that... it all looked good to me then i remembered to double check logic operators. so obvious yet so hidden... heh sneaky gotta love code... or you'l go crazy
Last edited on
lol oh yeah thanks, also what do you mean, how could I do it with just a while?

Is would you be able to re-post the code minus the extra line?
here would be how i would do it...

do what you like with it i included string and iostream because i am familiar with them. do what works i say.

i took it out of its function and put it in a main but it is the same idea just declare the function

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 <string>
#include <iostream>

using namespace std;

bool kill;
int main()
{
    string pass;

     while (kill == false)
     {
          cout << "Enter the password you would like to use,\n"
               << "it must conain atleast 5 letters, start with\n"
               << "a letter, contain atleast one capital letter\n"
               << "and have no punctuation.\n";
               getline (cin,pass);
            if (pass.length() >= 5 && pass.length() <= 16)
            {
            kill = true;
            }
            else if (pass.length() < 5)
                  {
                        cout << "Your password is too short! Must be atlest 5 characters.";
                  }
            else
                cout << "Your Passwork is too long. Please make it shorter.";

     }
}


oh remember to add \n in choice locations to give your function a pretty factor. ;)
Last edited on
Ok thanks a bunch good help!!!!!
Last edited on
No problem. i find that just looking at more code makes be a better programmer. The more lines i look at, the better i am. And it always gives me the question... How would i do that?
Topic archived. No new replies allowed.