How to keep program from ending

I have my program working for the most part except I don't want the program to quit working as soon as it finds a false statement but to ask me again to enter a password, so I was wondering if anyone can point me in the right direction to make this happen without messing up the program. Thanks in advance

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
#include <iostream>
#include <cstring>
	 
	using namespace std;
	 
	bool PasswordValid(const char *);
	 
	int main()
	{

	    char password[50];
	 
	   cout << "Please enter a password: ";
	   cin >> password;
	 
    if(!PasswordValid(password)) {
	        cout << "" << endl;
	    } else
	        cout << "Thank you, that is a valid password" << endl;
	 
	  return 0;
	}
	 
	bool PasswordValid(const char *password)
	{
	    
	    if(strlen(password) < 6) {
	        cout << "Password must be at least 6 characters long." << endl;
	        return false;
	    }
	 
	  
	    if(!strpbrk(password, "0123456789")) {
	        cout << "Password must include at least one digit (1-9)" << endl;
	       return false;
	    }
	 
before return 0; put std::cin.ignore();or if you are using namespace std do cin.ignore();
Thanks for reply but cin.ignore isn't doing what I want it to as I want the program to go back up and ask for the password again.
do
1
2
3
 while(true){
//your stuff 
}

that is a basic while loop
I feel like I should butt in here. :)

ul hlho's while loop could potentially go on forever. Loops like that are often used with an if and a break statement in their bodies, for example:

1
2
3
4
5
6
while(true) {
    //Stuff.
    if (/*condition that checks if the loop should end*/)
        break; //End the loop!
    //Potentially more stuff.
}


In your case, the stuff and more stuff would be std::cout and std::cin statements prompting for the password and possibly telling you that you entered the wrong one.

Good luck!
-Albatross
Last edited on
Topic archived. No new replies allowed.