944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 744
  • C++ RSS
Nov 20th, 2007
0

Hi to all, I am a newbi

Expand Post »
Hi, I am supose to do a program in C++ that tests for a password
it should do the following:

a- the password should be between 6 and nine characters
b- the password should contain at least one upper case and at least one lowercase letter
c- the password should contain at least one digit

the program works fine to check for requirements b and c but it does not work properly when I check the string length here is the code I got so far:
Thanks is advance
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. bool testlower( char [], int);
  8. bool testupper( char[], int);
  9. bool testdigit( char[],int);
  10. bool testnum(char[]);
  11.  
  12.  
  13.  
  14. int main()
  15. {
  16. int const size = 10; //can handle 9 characters
  17. char password[size];
  18.  
  19.  
  20. cout <<" Enter your password:" <<endl;
  21. cout <<" Make sure that its more than six characters\n";
  22. cout <<" and its less than 9 characters long.\n";
  23. cout <<" has at least one digit and \n";
  24. cout <<" at least one uppercase and lower case letter\n";
  25. cout <<" otherwise the program wont work proberly, thank you for you cooporation\n";
  26. cin.getline(password,size);
  27.  
  28.  
  29. if ((testlower( password, size) && testupper (password,size) && testdigit ( password,size)&& testnum(password))== true)
  30. cout <<"the password that you entered is valid\n";
  31.  
  32.  
  33. if ( testlower( password,size) == false )
  34. {
  35. cout<<" the password that you entered is invalid because there is\n";
  36. cout<<" no lower case letter in your password.\n";
  37. }
  38. if( testupper( password, size) == false)
  39. {
  40. cout<<" the password that you entered is invalid because there is\n";
  41. cout<<" no upper case letter in your password.\n";
  42. }
  43. if ( testdigit(password,size) == false)
  44. {
  45. cout<<" the password that you enterd is invalid because there is\n";
  46. cout<<" not digits in your password\n";
  47. }
  48. if (testnum(password)== false)
  49. {
  50. cout<<" the password that you entered is invalid because the number\n";
  51. cout<<" of characters is less than 6 or is more than 9 \n";
  52. }
  53.  
  54. return 0;
  55.  
  56.  
  57. }
  58. //-------------------------------------------------------
  59.  
  60. bool testlower(char password[], int size)
  61. {
  62. for ( int count=0; count < (size-1); count++)
  63. {
  64. if( islower(password[count]))
  65.  
  66. return true;
  67.  
  68.  
  69. }
  70. return false;
  71. }
  72. //--------------------------------------------------------
  73.  
  74. bool testupper( char password[], int size)
  75. {
  76. for ( int count=0; count < (size-1); count++)
  77. {
  78. if ( isupper( password[count]))
  79.  
  80. return true;
  81.  
  82.  
  83. }
  84. return false;
  85. }
  86. //-------------------------------------------------------
  87. bool testdigit( char password[], int size)
  88. {
  89. for ( int count=0; count < (size-1); count++)
  90. {
  91. if ( isdigit(password[count]))
  92.  
  93. return true;
  94.  
  95.  
  96. }
  97.  
  98. return false;
  99. }
  100. //this is where the code in not working properly I tried to modify it in many ways
  101. //but still can't get the right asnwer
  102.  
  103. bool testnum( char password[])
  104. {
  105. int length;
  106. length = strlen(password);
  107.  
  108. if( length < 6 || length <=9)
  109. {
  110. return false;
  111. }
  112.  
  113. return true;
  114. }
Last edited by Ancient Dragon; Nov 20th, 2007 at 9:15 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DamiLeon is offline Offline
2 posts
since Nov 2007
Nov 20th, 2007
0

Re: Hi to all, I am a newbi

>>but it does not work properly when I check the string length
Just add code to call strlen() to get the password length the check if it is <= 9. If you really wrote all that code you posted you should have no problem checking for length.
Last edited by Ancient Dragon; Nov 20th, 2007 at 9:19 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 20th, 2007
0

Re: Hi to all, I am a newbi

Another thing to consider, what if the user enters 15 characters? All you read are the first 9 and he thinks his password is longer than it is. You should probably accept more characters and give an error if 10 or more are entered.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Nov 21st, 2007
0

Re: Hi to all, I am a newbi

Quote ...
length < 6
I read length smaller than 6. I don't think you want that
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wambooman is offline Offline
3 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: RougeWave CTLib help
Next Thread in C++ Forum Timeline: "breaking up" your application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC