Dec 8, 2011 at 7:39pm UTC
Hello, I'm having a problem with this code I wrote. I'm supposed to enter a 8 character password, 5 letters and 3 numbers. If the password does not have 5 letters and 3 numbers, then it is an invalid password. I was able to complete the program but every time I run it, I get an error!! And I have checked and double checked the program for any errors but could not find any. Please help.
This is what I did..
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 62 63 64
#include <iostream>
#include <cctype>
using namespace std;
int Password(char [], int );
int Passcode(char [], int );
int main()
{
const int SIZE = 8;
char password[SIZE];
int num, num1;
cout << " Enter a password that is 8 characters long.\n" ;
cout << " The entered password must have 5 letters and 3 numbers." << endl;
cin.getline(password, SIZE);
num = Password(password, SIZE);
cout << "\n The number of digits entered is " << num << endl;
num1 = Passcode(password, SIZE);
cout << "\n The number of characters entered is " << num1 << endl;
if (num == 3 && num1 == 5)
{
cout << "\n\n You have entered a valid password." << endl;
}
else
cout << "\n\n You have entered an invalid password.\n" << endl;
return 0;
}
int Password(char pass [], int size)
{
int counter = 0;
for (int count = 0; count < size; count++)
{
if (isdigit(pass[count]))
{
counter++;
}
}
return counter;
}
int Passcode(char pass [], int size)
{
int counter = 0;
for (int count = 0; count < size; count++)
{
if (isalpha(pass[count]))
{
counter++;
}
}
return counter;
}
Last edited on Dec 8, 2011 at 7:40pm UTC
Dec 8, 2011 at 8:54pm UTC
Line 11:
const int SIZE = 8;
to
const int SIZE = 9;
You need a NULL on the end of it!
a b c d e 1 2 3 '\0' = 9 characters.
You should have added this after line 17. It would have showed you that it's taking 1 char off the end of what you type in:
cout << password << '\n' ;
Last edited on Dec 8, 2011 at 8:55pm UTC
Dec 8, 2011 at 10:41pm UTC
Thanks a lot! I thought it would work with 8 since the count started from 0 and stops at 7, and 8 would be '\0'
Dec 8, 2011 at 10:44pm UTC
lol, np. Looking at it now, I too would have put 8 in there... Never realised that earlier! ahaha