Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Hidden Password Error C++

0 votes
asked May 11, 2018 by Arslan K Q (100 points)
/*Hello everyone!!! I made this C++ program in which the program asks the user to enter their password twice and the program would compare the passwords. The password is desplayed in hidden form. The problem is that the true statement is shown as false, please help.*/

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

int main()

{

tryagain:

    cout<<"Enter Password: ";

    char pass[8];

    int i=0;

    char a;

    for(i=0;;)

    {

        a=getch();

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

        {

            pass[i]=a;

            ++i;

            cout<<"*";

        }

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

        {

            cout<<"\b \b";

            --i;

        }

        if(a=='\r')

        {

            pass[i]='\0';

            break;

        }

    }

    system("cls");

    cout<<"Enter Password again: ";

    char pass2[8];

    for(i=0;;)

    {

        a=getch();

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

        {

            pass2[i]=a;

            ++i;

            cout<<"*";

        }

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

        {

            cout<<"\b \b";

            --i;

        }

        if(a=='\r')

        {

            pass2[i]='\0';

            break;

        }

    }

    cout<<"\nYou entered: "<<pass<<"\n";

    cout<<"You entered: "<<pass2<<"\n";

    int p=0;

    for(int i=0; i<32; i++)

    {

    if((pass[i]==pass2[i])||(pass2[i]==pass[i]))

    {

    p==1;

    break;

}

}

if(p==1)

{

cout<<"Test\n";

goto end;

}

else

{

cout<<"Wrong\n";

goto tryagain;

}

end:

    return 0;

}

1 Answer

0 votes
answered May 17, 2018 by moses
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
/*Hello everyone!!! I made this C++ program in which the program asks the user to enter their password twice and the program would compare the passwords. The password is desplayed in hidden form. The problem is that the true statement is shown as false, please help.*/

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

#define MAXLENGTH 25

int main(){

    tryagain:

        cout<<"Enter Password: ";
        char pass[MAXLENGTH];
        int i=0;
        char a;

        for(i=0;i<MAXLENGTH;){
            a=getch();

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

                pass[i]=a;
                cout<<"*";
                ++i;

            }

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

                cout<<"\b \b";
                --i;

            }

            if(a=='\n'){//or \r (windows)

                pass[i]='\0';
                break;

            }

        }

    system("clear");//or cls for windows

    cout<<"Enter Password again: ";

    char pass2[MAXLENGTH];

    for(i=0;i<MAXLENGTH;){

        a=getch();

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

            pass2[i]=a;
            ++i;
            cout<<"*";

        }

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

            cout<<"\b \b";
            --i;

        }

        if(a=='\n'){

            pass2[i]='\0';
            break;

        }

    }

    cout<<"\nYou entered: "<<pass<<"\n";

    cout<<"You entered: "<<pass2<<"\n";

    int p=0;

    for(i=0; pass[i]!='\0' ; i++){

        if(pass[i]!=pass2[i]){

            p++;//u used p==1 which was a boolean operator and u were only testing for only first similarity, what if password is similar at the beginning and not at the end
            break;

        }

    }

    if(p==0){

        cout<<"Test\n";
        goto end;

    }else{

        cout<<"Wrong\n";
        goto tryagain;

    }

    end:

        return 0;

}

//This is a refractored part of ur code, ll find some comments
commented May 17, 2018 by anonymous
oh thanks man, Moses thank you so much you solved my problem
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.
...