Trying to write a simple code to compare password, but am unable to do

Can't get the coding right

Posted 01 October 2010 - 01:18 AM

I am trying to write a simple code to compare password, but am unable to do so.....
The program contains a class password which contains a constructor to initialize the actual password.......
there are also to other function...
the first one is to input the password
and the second one is to compare the input password to the actual one.....
If the two passwords are same then the program outputs "Correct Password"
else it says "incorrect Password"

The Problem: Whenever I run the program,TC++ crashes though there are no errors and warnings.....
the window just closes......
I think there must be a logical error....
But i cannot find where.....
Please guys helm me find out.....

Re: Can't get the coding right

but what is i?
we have exited the for loop......
do u mean at the end of the string i should insert the null character.....
but how does it help in the code?

The null character makes the difference between a char array and a string. When a function reads the null character this tells it it has reached the end. Say as as if

password[]="cheese";

Then

password[0] is 'c'
password[1] is'h'
...
password[5] is 'e'
password[6] is '\0' reached end of the string, stop reading it;
as password[7] is random data and you don't want strcmp to compare it to the input etc.

Hope this helps!

edit:

i is just the next value. the loop increments and then tests. so password[i] is the next character.

and change line 43 of main toif(!pass.comp())
notice the not.
if they are equal strcmp will return 0 which is false.

i made a mistake:
void password::input()
should be i-1 NOT i see previous post!

Re: Can't get the coding right

It helps the code because at present you're comparing "helloeshgshgs" with "helloh[sttsth[sh-ttijhnnn]inn" (or some other random garbage past the end of what you consider to be the real strings).

Until you make sure you add the \0 in the correct place for BOTH strings, you're not going to succeed with the strcmp().

BTW, what does strcmp() return when two strings are equal (not what you think at present).

Oh and I might suggest you ditch the 20+ year old TurboC++ compiler in favour for something belonging to this millenium. But I'm guessing your school forces obsolete tech on you from the start. Think about that and how well that is likely to prepare you for your career.