/******************************************************************************
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