Help With A Password Verifier
-
Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help
// LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }
-
Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help
// LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }
LilKoopa wrote:
while (length != 10)
Your code would only work if the length of the "pass" string is returned as 10. You need to check the condition in the while loop first
LilKoopa wrote:
cout << length <<endl;
This line, above the while loop would be printing 32 ie the size of pass variable since the length would always be greater than 10
LilKoopa wrote:
so that I can check if the program actually works
I hope that you can now check the program after modifying the condition for the while loop
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Hi again I was wondering if somebody could help me with this program I cant get it to stop displaying 32 so that I can check if the program actually works. here is my code. Thanks in advance if anyone can help
// LanusPassword.cpp : Defines the entry point for the console application. //Corey Lanus //Program # 10 //Lanus Password //Verifies that the password meets the requirements //November 10, 2008 #include "stdafx.h" #include #include #include using namespace std; int main() { const int size=10; string pass; int length; int caps=0; int num=0; int low=0; cout << "Please enter a 10 character password.\n"; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; length = sizeof(pass); cout << length <<endl; while (length != 10) { cout << "You can only enter 10 characters. Please type again.\n"; cin >> pass; length = sizeof(pass); } for (int cnt=0; cnt<size;>{ cout << pass[cnt] << " "; } // New line cout << endl; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } while (caps<2 || low < 1 || num < 1) { num=0; caps=0; low=0; cout << "You must make sure your password has at\n"; cout << "least two uppercase and at least one\n"; cout << "lowercase letter and atleast 1 number.\n"; cin >> pass; for (int i=0; i<size;>{ cin >> pass[i]; if (isdigit(pass[i])) num=num+1; if (isupper(pass[i])) caps=caps+1; if (islower(pass[i])) low=low+1; } } cout << "Your password: " << pass << ", is good and excepted.\n"; return 0; }
sizeof(pass) always returns 4 - it's returning the size in bytes of the pointer that points to the string, not the actual length of the string itself.. if you change string pass; to char pass[100]; and all occurances of sizeof(pass) to strlen(pass) You'll get code that correctly counts the number of letters in the password. The logic in your code then gets a bit funky, and I can't quite tell what you're trying to do there. Often things become much more clear when you separate out the individual parts of a problem into their own functions. Might I suggest a different approach to this problem?
#include <iostream>
using namespace std;int countNumChars(char *str)
{
//
// TODO: add code for this function
//
//
}int countLowerChars(char *str)
{
//
// TODO: add code for this function
//
//
}int countUpperChars(char *str)
{
//
// TODO: add code for this function
//
//
}bool isValidPassword(char *password)
{
int len, numberChars, lowerCaseChars, upperCaseChars;
bool result = false;
len = strlen(password);
numberChars = countNumChars(password);
lowerCaseChars = countLowerChars(password);
upperCaseChars = countUpperChars(password);
result = ((len==10)&&(lowerCaseChars>=1)&&(upperCaseChars>=2)&&(numberChars>=1));
return result;
}int main()
{
char pass[100];cout << "Please enter a 10 character password.\\n"; cout << "You must make sure your password has at\\n"; cout << "least two uppercase and at least one\\n"; cout << "lowercase letter and at least 1 number.\\n"; cin >> pass; while (!isValidPassword(pass)) { cout << "Invalid Password. Please type again.\\n"; cin >> pass; } cout << "Your password: " << pass << ", is good and accepted.\\n"; system("pause"); return 0;
}