Login Program which Inputs the Password in Hidden Form, but the correct Login is shown Incorrect.

–1 vote
asked Jun 10, 2018 by Arslan K Q (160 points)
/*This is a C++ Program. This Program asks the User to Enter their Username and Password

(Password is Input in Hidden form"*"), after doing so the Program compares that is Username equals to "admin" and

is password equals to "abc123".*/

//The Problem is that it shows that the Login is not correct even though I Input the correct information.

#include<iostream>

#include<conio.h>

#include<stdlib.h>

#include<string>

using namespace std;

int main()

{

  tryagain:

  string username;

  cout<<"please Enter your Username: ";

  getline(cin, username);

  cout<<"Please Enter Your Password: ";

  char password[32];

  int i=0;

  char a;

  for(i=0;;)

  {

    a=getch();

    if((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))

    {

      password[i]=a;

      ++i;

      cout<<"*";

    }

    if(a=='\b'&&i>=1)

    {

      cout<<"\b \b";

      --i;

    }

    if(a=='\r')

    {

      password[i]='\0';

      break;

    }

  }

  cout<<"\nYour username is: "<<username;

  cout<<"\nYour Password is: "<<password;

  if(username=="admin"&&password=="abc123")

  {

  cout<<"\nWelcome\n";

  }

  else

  {

  cout<<"\nWrong Username or Password, please Try again\n";

  goto tryagain;

  }

  return 0;

}

1 Answer

0 votes
answered Jun 12, 2018 by Michał Ostrowski (140 points)
edited Jun 12, 2018 by Michał Ostrowski

I'm not sure but Maybe you compare string with poiter to char[]  here:

  if(username=="admin"&&password=="abc123")

Try:

#include<cstring>

 if(username=="admin"&&( strcmp (password,"abc123"))!=1 )

Or do it by char.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...