"Checkpassword homework"

Hey guys, so I have a homework assignment where it consists of using username and passwords. Im working on a method to check the password to verify that the password entered does not contain a space and is not shorter than 5 characters. I created this method and it worked, but my professor said I need to change it to a do while loop which I absolutely can not stand. I am stuck in an infinite loop of not knowing what to do after working on it for 3 hours. I am going to paste my code (I suggest copying and pasting it in a new java file since there are a lot of comments). Hopefully you guys can help me get a final understanding of how I do this.
Thanks,
--Kodi
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
61
 string CheckPassword(){
	char ch;
	string pass;
	bool flag = false;
		   pass = "";			// initialize to null/empty string
           cout << " Enter your Password:\n";
           ch = _getch();

		  /* while(ch != 13){
			//character 13 is enter - carriage return
				if (isspace(ch)){
					cout << "\n A space is not a valid Password character.\n";
					cin.clear();
					cin.ignore();
					CheckPassword();
				}
				else{
					pass += ch;
					cin.putback(ch);
					cout << '*';
					ch = _getch();
				}
			}
		   */
		   do{
			   if (ch == 13)
					flag = true;

			   else if (isspace(ch)){
				   cout << "\n A space is not a valid Password character.\n";
				   cin.clear();
				   flag = false;
			   }
			   else{
				    pass += ch;
					cin.putback(ch);
					cout << '*';
					ch = _getch();
					flag = false;
			   }

			   if (pass.length() < 5){
				   cout << "\n The length of the Password must exceed 5 Characters.\n";
				   cin.clear();
				   flag = false;
			   }
		   }
		   while (flag == false);


			/*if (pass.length() < 5){
				cout << "\n The password is not long enough.\n";
				cin.clear();
				cin.ignore();
				CheckPassword();
			}*/
			cout << endl;
		    cin.clear();				// reset the input stream 
            cin.ignore();			// remove any remaing characters from the stream
	return pass;
}
Last edited on
The only way you can leave your do while loop is on line 26,27. Your write up says can't be shorter than 5 and spaces are illegal so that is all we need to test for.

I am sure there is a more graceful way to solve this, however hope this points you in a direction that helps.


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

using namespace std;

int main(){

	string password;
	bool flag;

	do{

		unsigned int i = 0;//Reset
		flag = true;//Reset flag

		cout << "Enter password: ";
		getline(cin, password);
		cout << endl;

		if (password.length() < 5){
			cerr << "Password too short." << endl;
			flag = false;//Too short
		}

		while (i < password.length() && flag){
			if (password.at(i) == ' '){
				cerr << "Password can not contain spaces;" << endl;
				flag = false;//Contains spaces
			}
			i++;
		}

	} while (!flag);

	cout << "Good Password!" << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.