User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 12 Nov 2003, 16:30   #1
Belgarath The Sorcerer
First Disciple of Aldur
 
Belgarath The Sorcerer's Avatar
 
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
Belgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud of
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.
Belgarath The Sorcerer is offline   Reply With Quote
Unread 12 Nov 2003, 16:54   #2
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
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
queball is offline   Reply With Quote
Unread 12 Nov 2003, 17:18   #3
djbass
mmm.. pills
 
djbass's Avatar
 
Join Date: Apr 2000
Location: Australia
Posts: 2,152
djbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond reputedjbass has a reputation beyond repute
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.
djbass is offline   Reply With Quote
Unread 12 Nov 2003, 17:49   #4
Belgarath The Sorcerer
First Disciple of Aldur
 
Belgarath The Sorcerer's Avatar
 
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
Belgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud of
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.
Belgarath The Sorcerer is offline   Reply With Quote
Unread 12 Nov 2003, 20:18   #5
queball
Ball
 
queball's Avatar
 
Join Date: Oct 2001
Posts: 4,410
queball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so littlequeball contributes so much and asks for so little
Re: c++ help

But what if someone enters xx11bb?
__________________
#linux
queball is offline   Reply With Quote
Unread 12 Nov 2003, 21:03   #6
Flavius
 
Join Date: Jan 2002
Posts: 421
Flavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet societyFlavius is a pillar of this Internet society
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
Flavius is offline   Reply With Quote
Unread 12 Nov 2003, 21:18   #7
JetLinus
Friendly geek of GD :-/
 
JetLinus's Avatar
 
Join Date: Nov 2000
Location: On my metal roid
Posts: 923
JetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud ofJetLinus has much to be proud of
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! :-/
JetLinus is offline   Reply With Quote
Unread 13 Nov 2003, 00:07   #8
Belgarath The Sorcerer
First Disciple of Aldur
 
Belgarath The Sorcerer's Avatar
 
Join Date: Jul 2000
Location: The Vale of Aldur
Posts: 1,470
Belgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud ofBelgarath The Sorcerer has much to be proud of
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.
Belgarath The Sorcerer is offline   Reply With Quote
Unread 14 Nov 2003, 04:04   #9
Entium
Registered User
 
Entium's Avatar
 
Join Date: Dec 2001
Location: Confœderatio Helvetica
Posts: 323
Entium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud ofEntium has much to be proud of
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)
Entium is offline   Reply With Quote
Unread 4 Dec 2003, 23:46   #10
Cyp
∞+♪²
 
Join Date: Nov 2000
Location: :uo!te]o¯|
Posts: 428
Cyp is an unknown quantity at this point
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-->
Cyp is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 15:01.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018