Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   password prog (http://www.go4expert.com/forums/password-prog-t21347/)

thapchi 13Mar2010 17:45

password prog
 
Can anybody help me with this password prog

I want this working but it says i cant store char [20]

it only takes first word like if i type passs word as hello it will take h

Code:

#include<iostream>
using namespace std;

int main()
{
    char newpass[20];
    char p[20];
    char pass[20];
    cout<<"Enter your new pass word = ";
        cin>>newpass;
       
    cout<<"You entred this password = "<<newpass;
    p=newpass;
   
    cout<<"\n Enter the password = ";
    cin>>pass;
   
    if(p==pass)
    {
        cout<<"\n Password is correct";
        getchar();
    }
    else
    {
        cout<<"it was incorrect";
        getchar();
    }
    getchar();
    return 0;
}


javor 13Mar2010 20:31

Re: password prog
 
Try to use string class for that

thapchi 14Mar2010 11:18

Re: password prog
 
waiting for more replays

xpi0t0s 14Mar2010 14:26

Re: password prog
 
The string class implements the syntax you're trying to use, i.e. assigning strings with = and comparing them with ==. If you don't want to use the string class then you need to use strcpy and strcmp instead.

p=newpass won't work because p is const. And p==pass will evaluate FALSE because these pointers point to different places. p==pass compares the pointers, not the string contents.

techgeek.in 14Mar2010 17:00

Re: password prog
 
Code:

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
    char newpass[20];
    char p[20];
    char pass[20];
    cout<<"Enter your new pass word = ";
        cin>>newpass;
       
    cout<<"You entred this password = "<<newpass;
    p=newpass;
   
    cout<<"\n Enter the password = ";
    cin>>pass;
   
    if(strcmp(p,pass)==0)
    {
        cout<<"\n Password is correct";
        getchar();
    }
    else
    {
        cout<<"it was incorrect";
        getchar();
    }
    getchar();
    return 0;
}


javor 14Mar2010 18:27

Re: password prog
 
I thought that way, I think is better for c++

Code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string newpass;
string p;
string pass;
cout<<"Enter your new pass word = ";
cin>>newpass;

cout<<"You entred this password = "<<newpass;
p=newpass;

cout<<"\n Enter the password = ";
cin>>pass;

if(p==pass)
{
cout<<"\n Password is correct";

}
else
{
cout<<"it was incorrect";

}

return 0;
}



All times are GMT +5.5. The time now is 20:18.