|
12 Nov 2003, 16:30
|
#1
|
First Disciple of Aldur
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
|
c++ help
I have a program, protected by a password. It's pretty simple, in that it all it does is ask the user to input a password, which is compared to the correct password, which is just an interger.
Now we have to modify the program such that the user enters a password in the format 'xx11', we have to validate that the the user is entering two letters and two numbers, and then compare it to the password.
As you may have guessed by me posting here, I'm a little stuck, here's the code I have:
const char INITIALS[2]="xx";
const int NUMBER[2]={11};
Code:
for(PasswordAttempts=MAX_PWORD_ATTEMPTS; PasswordAttempts>0 && !PasswordValid; PasswordAttempts--)
{
cout << "Please enter password... ";
cin >> Password;
// this is the bit i'm a bit stuck on
Initials[0]=Password[0,1];
Number[0]=Password[2,3];
strcmp(Initials,INITIALS)==0;
if(isalpha(Password[0]) && isalpha(Password[1]) && isdigit(Password[2]) && isdigit(Password[3]) == (Initials && Number)
{
PasswordValid=true;
}
else
{
cout << "\nPassword invalid, you have " << PasswordAttempts - 1 << " attempts left." << endl;
}
}
if(PasswordValid==true)
{
do
{
rest of program, etc...
Can anyone shed any light, or at least point me in the right direction?
__________________
Yeah.
|
|
|
12 Nov 2003, 16:54
|
#2
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Re: c++ help
Wouldn't it be easier to validate and then just do a string compare to "xx11"?
Initials[0]=Password[0,1]; is not what you want. You could copy each character seperately , but I don't see what you're achieving. I'm guessing Password is a string (istream<< into a char* is dangerous - use get instead if you can't use C++ strings). After validating the password you could use atoi to get the number part if you need it.
Other things: INITIALS isn't null terminated (it holds two characters and is only 2 chars big), so don't use strcmp, but you could use strncmp. You should check the size of the password somewhere.
__________________
#linux
|
|
|
12 Nov 2003, 17:18
|
#3
|
mmm.. pills
Join Date: Apr 2000
Location: Australia
Posts: 2,152
|
Re: c++ help
Code:
const char Pass[4] = {'x', 'x', '1', '1', 0};
if(isalpha(Password[0]) && isalpha(Password[1]) && isdigit(Password[2]) && isdigit(Password[3]) && (strcmp(Password, Pass)==0))
Would do the trick I suspect, and queball is right there's a lot of redundant code in there that doesn't actually do anything useful.
You could get rid of:
const char INITIALS[2]="xx";
const int NUMBER[2]={11};
Initials[0]=Password[0,1];
Number[0]=Password[2,3];
strcmp(Initials,INITIALS)==0;
=[DJ Bass]=
__________________
CSS : the result of letting artists design something only an engineer should touch.
Last edited by djbass; 12 Nov 2003 at 17:26.
|
|
|
12 Nov 2003, 17:49
|
#4
|
First Disciple of Aldur
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
|
Re: c++ help
queball's reply helped immensely as it happens. I now I have:
Code:
if(isalpha(Password[0]) && isalpha(Password[1]) && isdigit(Password[2]) && isdigit(Password[3]) && (strncmp(Password,FIXED_PASSWORD,4) == false))
where FIXED_PASSWORD is obviously the correct password
Everything works as it should, and does exactly what the question asked me to do, although I'm not sure that this is the exact way that she wants us to do it, but it works, heh.
thanks guys.
__________________
Yeah.
|
|
|
12 Nov 2003, 20:18
|
#5
|
Ball
Join Date: Oct 2001
Posts: 4,410
|
Re: c++ help
But what if someone enters xx11bb?
__________________
#linux
|
|
|
12 Nov 2003, 21:03
|
#6
|
Join Date: Jan 2002
Posts: 421
|
Re: c++ help
then just check that first 4 chars are the same, and then check that size of stored password <= size of input password
edit: <= in case its the same
|
|
|
12 Nov 2003, 21:18
|
#7
|
Friendly geek of GD :-/
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
|
Re: c++ help
Quote:
Originally Posted by queball
But what if someone enters xx11bb?
|
needs a length-check as well then, i'd guess...
Edit: Ah well, been a bit late with that idea ^^
__________________
[ »] Entropy increases! :-/
|
|
|
13 Nov 2003, 00:07
|
#8
|
First Disciple of Aldur
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
|
Re: c++ help
I'll have a go at that tomorrow, I need to put some more validation in anyway, so I can get it all done at the same time.
Thanks for the help lads.
__________________
Yeah.
|
|
|
14 Nov 2003, 04:04
|
#9
|
Registered User
Join Date: Dec 2001
Location: Confœderatio Helvetica
Posts: 323
|
Re: c++ help
Quote:
Originally Posted by JetLinus
needs a length-check as well then, i'd guess...
|
int len(string) ==4 (?)
And/Or you could try something like
char charAT(string, pos) == char charAT(initials, pos)
|
|
|
4 Dec 2003, 23:46
|
#10
|
∞+♪²
Join Date: Nov 2000
Location: :uo!te]o¯|
Posts: 428
|
Re: c++ help
Code:
#define a PasswordAttempts
#define b PasswordValid
#define e long
#define f MAX_PWORD_ATTEMPTS
#define g goto h
const char INITIALS[2]="xx";
const int NUMBER[2]={1,1};
for(a=f;a>0&&!b;--a){
char d[8224];
cout<<"Please enter password... ";
cin>>d;
e i;b=1;
e c=8224|(i=*(e*)d);
if(2155905152u&c)g;
c-=808477025;
if(2155905152u&c)g;
c-=151591194;
if(2155905152u&~c)g;
if(d[4])g;
++b;h:
if(!(b&&i==((e)INITIALS[0]|((e)INITIALS[1]<<8)|(((e)NUMBER[0]+48)<<16)|(((e)NUMBER[1]+48)<<24)))){
if(b)cout<<"\nPassword invalid, you have "<<PasswordAttempts-1<<" attempts left."<<endl;else cout<<"\nInvalid password format, you have "<<PasswordAttempts-1<<" attempts left."<<endl;
b=0;}
if(PasswordValid==true)
{
do
{
rest of program, etc...
This checks some things in parallel...
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics... 猫
<!--Last incarnation: Nov 2000-->
|
|
|
|
All times are GMT +1. The time now is 20:44.
| |