Click here to Skip to main content
15,895,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

Please Help me i am trying to make password Field and after taking password from user i want to match it with Stored password.if Enter password is Same then it call the next function.

Problem
this program take password from the user until it enter.after enter it compare with store password but it did not match the stored password but i enter the right password and always give me error "password is not match" please help me if any one know about this.

C++
    bool Choice;
    char Con;
    while(Choice!=true)
    {
        Header();
        cout<<setw(45)<<"Enter Name:";
        cin>>Name;
        cout<<endl<<endl<<endl;
        cout<<setw(49)<<"Enter Password:";
        while(Con!=13)
        {
            Con=getch();
            Password+=Con;
            cout<<"*";
        }
        if(Password.compare("Qasim_365")==0)
        {
        cout<<endl<<endl<<endl
            <<setw(56)<<"Account Access Granted."<<endl;
        system("pause");
        system("cls");
//Function Atfter Password Same
        AdminMenu();
        break;
        }
        else
        {
            Con=0;
            cout<<"\n\n\t\t\t\tPassword Not Matched Try Again."<<endl;
            system("pause");
            system("cls");
            Choice=false;
        }
    }
Posted
Updated 9-Jan-15 5:12am
v2
Comments
Richard MacCutchan 9-Jan-15 11:15am    
Probably because you forgot to add a null ('\0') at the end of Password.
Sergey Alexandrovich Kryukov 9-Jan-15 13:04pm    
Never ever store passwords anywhere. This is unsafe and absolutely not needed to authentication. Standard approach is to store cryptographic has function of a password, it does not allow to figure out the password itself, but allows to compare — hash with hash.
—SA
Geoffrey Mellar 9-Jan-15 18:03pm    
I agree you shouldn't store passwords. Maybe you could use std::getline to read the password.

1 solution

Correct me if I'm wrong ( I haven't used char in FOREVER ), but isn't he passing a single letter to password then rewriting it with the next loop?

char con;  <small>Single Charactor</small>
while(Con!=13) <small>While Con != CR</small>
        {
            Con=getch();
            Password+=Con;  //Not sure how Password is defined but doesn't seem appropriate for adding to a string
            cout<<"*";
        }
int i = 0;
while(Con!=13){
     Con=getch();
     Password[i] = Con;
     cout<<"*";
     i++;
}
     Password[i] = '\0'


You could use GetLine().

std::string Password;
  std::cout << "Please, enter your password: ";
  std::getline (std::cin,password);
password.compare("Quasim...........


Although I am assuming your doing single character so you can replace it with a '*'

Also it's never really a good idea to use system calls. Windows has a sleep() function to pause and Clearing the screen is a little more difficult
#include <windows.h>
#include <iostream>

int main(int argc, TCHAR argv[])
{

    HANDLE hStdout;

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    cls(hStdout);

}

void cls( HANDLE hConsole )
{
   COORD coordScreen = { 0, 0 };    // home for the cursor 
   DWORD cCharsWritten;
   CONSOLE_SCREEN_BUFFER_INFO csbi; 
   DWORD dwConSize;

    // Get the number of character cells in the current buffer. 

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;
   dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

   // Fill the entire screen with blanks.

   if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Get the current text attribute.

   if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
      return;

   // Set the buffer's attributes accordingly.

   if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten ))
      return;

   // Put the cursor at its home coordinates.

   SetConsoleCursorPosition( hConsole, coordScreen );
}</iostream></windows.h>


http://www.cplusplus.com/forum/beginner/1988/3/#msg10830[^] Again Windows

http://www.cplusplus.com/forum/beginner/43683/[^]

Shows how to hide your text so you could use getline(). Remember Google is everybodys best friend, because every problem you will encounter, good chance some one already did and posted a solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900