Can't get the coding right

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

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2034 Views - Last Post: 05 October 2010 - 04:12 AM Rate Topic: -----

#1 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

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.....

The code is:
#include <iostream.h>
#include <conio.h>
#include <string.h>
class password
	{
	char pass[20];             		//Stores Actual Password
	char inpass[20];                        //Stores Input Password
   public:
	password(char []);                      //Initializes Actual Password
	void input();                           //Inputs the Password
	int comp();                             //Compares the Password
	};
void password::password(char p[])
	{
	int len=strlen(p);
	for(int i=0;i<len;i++)
		{
		pass[i]=p[i];
		}
	}
void password::input()
	{
	char temp;
	cout<<"Enter Password(!to end): ";
	for(int i=0;temp!='!';i++)
		{
                temp=getch();                    //After first Edit
		inpass[i]=temp;                  /
		cout<<"*";
		}
	}
int  password::comp()
	{
	int temp;
	temp=strcmp(pass,inpass);
	return temp;
	}
void main()
	{
        password pass("password");
	clrscr();	
	pass.input();
	if(pass.comp())
		cout<<"\nCorrect Password";
	else
		cout<"\nIncorrect Password";
	getch();
	}


This post has been edited by sumit.xxx365: 01 October 2010 - 02:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't get the coding right

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Can't get the coding right

Posted 01 October 2010 - 01:36 AM

So where is the cin >> temp;
in your input function?
Was This Post Helpful? 1
  • +
  • -

#3 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Can't get the coding right

Posted 01 October 2010 - 01:40 AM

oh thanks.....
I think i missed that.....
temp=getch();
but now the new edit give "correct password to all password".... :/

This post has been edited by sumit.xxx365: 01 October 2010 - 01:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Can't get the coding right

Posted 01 October 2010 - 01:54 AM

At what character does strcpy() stop at?

Are any of your string manipulations dealing with this character?
Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Can't get the coding right

Posted 01 October 2010 - 02:04 AM

Don't use void main()!!!!
Was This Post Helpful? 0
  • +
  • -

#6 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Can't get the coding right

Posted 01 October 2010 - 02:07 AM

View PostSalem_c, on 01 October 2010 - 12:54 AM, said:

At what character does strcpy() stop at?

Are any of your string manipulations dealing with this character?

have not used strcpy() anywhere.....
do u mean strcmp()?
It just compares the string and returns 0 for false and 1 for true....(isn't that so?)

and I don't know what u mean by string manipulation....

View Postsimeesta, on 01 October 2010 - 01:04 AM, said:

Don't use void main()!!!!

umm why?
what benifits does using int main() and returning 0 have over void main?
Was This Post Helpful? 0
  • +
  • -

#7 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Can't get the coding right

Posted 01 October 2010 - 02:13 AM

This is why.

Null terminate your string
inpass[i]='\0'; after you have got it from user input.
Was This Post Helpful? 0
  • +
  • -

#8 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Can't get the coding right

Posted 01 October 2010 - 02:43 AM

View Postsimeesta, on 01 October 2010 - 01:13 AM, said:

This is why.

Null terminate your string
inpass[i]='\0'; after you have got it from user input.


umm sorry for my noobiness but i still don't understand what u mean..... :/
Was This Post Helpful? 0
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Can't get the coding right

Posted 01 October 2010 - 02:48 AM

void password::input()
	{
	char temp;
	cout<<"Enter Password(!to end): ";
	for(int i=0;temp!='!';i++)
		{
                temp=getch();                    //After first Edit
		inpass[i]=temp;                  /
		cout<<"*";
		}
        inpass[i-1]='\0'; //add this line to your code!
	}



and
void password::password(char p[])
	{
	int len=strlen(p);
	for(int i=0;i<len;i++)
		{
		pass[i]=p[i];
		}
        pass[i]='\0'; //add this too
	}



it will add a '\0' to the end of the array telling strcmp this is the end of it. '\0' is NULL and adding to the end of char array makes it a string.

This post has been edited by simeesta: 01 October 2010 - 03:29 AM

Was This Post Helpful? 1
  • +
  • -

#10 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Can't get the coding right

Posted 01 October 2010 - 02:54 AM

View Postsumit.xxx365, on 01 October 2010 - 03:43 AM, said:

View Postsimeesta, on 01 October 2010 - 01:13 AM, said:

This is why.

Null terminate your string
inpass[i]='\0'; after you have got it from user input.


umm sorry for my noobiness but i still don't understand what u mean..... :/

Simeesta is right, if you want to be a good programmer than use int main and have it return 0; it's the standard and is much better.
Was This Post Helpful? 0
  • +
  • -

#11 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Can't get the coding right

Posted 01 October 2010 - 02:54 AM

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?
Was This Post Helpful? 0
  • +
  • -

#12 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Can't get the coding right

Posted 01 October 2010 - 03:00 AM

View Postsumit.xxx365, on 01 October 2010 - 10:54 AM, said:

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 to
if(!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!

This post has been edited by simeesta: 01 October 2010 - 03:34 AM

Was This Post Helpful? 1
  • +
  • -

#13 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Can't get the coding right

Posted 01 October 2010 - 04:23 AM

*
POPULAR

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.
Was This Post Helpful? 6
  • +
  • -

#14 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 215
  • View blog
  • Posts: 586
  • Joined: 04-August 09

Re: Can't get the coding right

Posted 01 October 2010 - 04:38 AM

If you will stop using turbo C++ i can recommend Codeblocks - with MinGW, an IDE for C and C++. You can download it here.
Was This Post Helpful? 0
  • +
  • -

#15 sumit.xxx365  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 15
  • Joined: 26-September 10

Re: Can't get the coding right

Posted 01 October 2010 - 08:38 PM

Well I tried to implement the stuff u suggested....
but it's still not working...... :/

#include <iostream.h>
#include <conio.h>
#include <string.h>
class password
    {
    char pass[20];                         //Stores Actual Password
    char inpass[20];                        //Stores Input Password
   public:
    password(char []);                      //Initializes Actual Password
    void input();                           //Inputs the Password
    int comp();                             //Compares the Password
    };
void password::password(char p[])
    {
    int len=strlen(p);
    for(int i=0;i<len;i++)
        {
        pass[i]=p[i];
        }
    pass[len]='\0';
    }
void password::input()
    {
    int t;
    char temp;
    cout<<"Enter Password(!to end): ";
    for(int i=0;temp!='!';i++)
        {
        t++;
        temp=getch();
        inpass[i]=temp;
        cout<<"*";
        }
    pass[t]='\0';
    }
int  password::comp()
    {
    int temp;
    temp=strcmp(pass,inpass);
    return temp;
    }
void main()
    {
password pass("password");
    clrscr();    
    pass.input();
    if(pass.comp())
        cout<<"\nCorrect Password";
    else
        cout<"\nIncorrect Password";               //This Line has no effect ://>
    getch();
    }



The warning that come are:
Line 50 has no effect...

This post has been edited by sumit.xxx365: 01 October 2010 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2