-
Code:
#include <stdio.h>
#include <conio.h>
void getpass(char*);
int main(){
char tpass[9];
printf("\nPlease enter your password:");
getpass(tpass);
while (tpass != "password"){
printf("\n\aWrong password! please try again:");
getpass(tpass);
}
}
/***********************************************/
void getpass(char* pass){
/* function variables */
char ch;
int flag = 0, i = 0;
/* function process*/
while(flag == 0){
ch = getch();
if (ch == '\r'){
if (i != 0){
pass[i] = NULL;
flag = 1;
continue;
}
printf("\a");
continue;
}
else if (ch == '\b'){
printf("\b");
i--;
continue;
}
printf("*");
pass[i] = ch;
i++;
}
}
I dont know why it go to while loop when I enter the pass word correctly!
this is a part of my whole code
-
while (tpass != "password"){
should be
while (strcmp(tpass, "password") != 0){
-
"password" is a string literal, which is const char*, so you try to compare an array (which becomes char*) to a const char*, which you understand, will never be true, since they are two distinct addresses.
You need to use the strcmp string function for comparing strings in C. Welcome to C.
-