Arrays and string

Hello,
I am new to the whole programming thing and have been trying to make program that will prompt the user to input a 5 character password and follow some rules like certain characters must be numbers, others must be lowercase or uppercase. I have to use an array and Im not really sure how to do it. Ive been trying for the last few days but every thing i tried didn't work out. Any help would be appreciated.

The actual assignment is as follows...

Write a program that will accept a password into a character array string variable. Test for it being a valid password (as per the rules below) and print a message indicating if the entered password is valid or invalid. The rules for an acceptable password are:

the first character must be the letter 'A', 'B', or 'C'
the second character must be a number from 0 -9
the third character must be a number from 0 - 9
the fourth character must be a lower case letter
the fifth character must be a number from 5 - 9
Here is the way to read the password into a char array.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main ()
{
  char password[6] = {0};

  cout << "Enter password (max 5) : ";
  cin.getline (password, 6);
}
Thank you! In doing this is each character of the password assigned to password[0], password[1]...etc. still? if so, any idea how I would make each character follow its own rule?
It is, including the null term at password[5]. You could use an if statement with logical OR || to check the rule at index 0. For the rules at indexes 1-4 try casting the character as an int and then checking that value for the corresponding ASCII decimal values. The condition for password[2], for example, would be
if(!(password[2] > 47 && password[2] < 58))
The C++ standard library does support regular expressions too.
http://www.cplusplus.com/reference/regex/regex_match/

An expression to match the first three characters by your rules would be something like:
"^[ABC][0-9][0-9]"
However, that is probably not what you are looking for.

The cin.getline is described here: http://www.cplusplus.com/reference/istream/istream/getline/

Billy's way to use ASCII table works, but (1) it is hard to see what 47 means and (2) can we always trust it to be that?

An alternative is to have constant "words" and search from them.
1
2
3
const char * rule1 = "ABC";
const char * rule2 = "0123456789";
const char * rule3 = "56789";

Now you are looking at one character of input.
Can it be found from word that represents a rule?
In other words, is character X equal to any character in word Y?

Then there are functions, like:
http://www.cplusplus.com/reference/locale/isalpha/
http://www.cplusplus.com/reference/locale/isdigit/
http://www.cplusplus.com/reference/locale/islower/
In doing this is each character of the password assigned to password[0], password[1]...etc. still? if so, any idea how I would make each character follow its own rule?


Yes. To validate each character you need to check the conditions;
the first character must be the letter 'A', 'B', or 'C'


if (password[0] != 'A' || password[0] != 'B' || password[0] != 'C')
// password invalid

for other conditions you can use isdigit() , islower()

http://www.cplusplus.com/reference/cctype/
Last edited on
Topic archived. No new replies allowed.