password prog

Discussion in 'C++' started by thapchi, Mar 13, 2010.

  1. thapchi

    thapchi New Member

    Joined:
    Mar 9, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
    
     
  2. javor

    javor New Member

    Joined:
    Mar 13, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Try to use string class for that
     
  3. thapchi

    thapchi New Member

    Joined:
    Mar 9, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    waiting for more replays
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  5. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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;
    }
     
    Last edited by a moderator: Mar 14, 2010
  6. javor

    javor New Member

    Joined:
    Mar 13, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     
    Last edited by a moderator: Mar 14, 2010

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice