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
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;
elseif (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;
}
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.