Hello everybody! I am just a beginner in C, so i have some problems with the password input . This is for bank account, it asks user for password input, but it doesn't works properly for three attempts, in order to go to menu: Could you help me please, what is the problem in? the piece of code is here:

attempt = 0; 
	while((attempt<MaxAttempt)&&!correct)
	{
	  
      attempt = attempt+1;
      puts ("\nEnter your password");
      fflush(stdout);
      while ((che = getch()) != EOF && che != '\n' && che != '\r' && i < sizeof(pword) - 1)
      {
          if (che == '\b' && i > 0) 
            {
               printf("\b \b");
               fflush(stdout);
               i--;
               pword[i] = '\0';
            }
            else if (isalnum(che))
            {
                 putchar('*');
                 pword[i++] = (char)che;
            }
     }
     pword[i] = '\0';
     strcpy(word,pword);
     printf ("\nYou entered >%s<", word);
 
        fseek(members, sizeof(account)*here, SEEK_SET);
	fread(buffer, sizeof(account),1,members);
	printf("%s %s", buffer->acc_name, buffer->pin);
   
        if (!strcmp(word, buffer->pin))
		{
			printf("Access granted\n\n\n");
			correct=TRUE;
			break;
			goto menu;
		} 
		else
		{	 
 			if(attempt<MaxAttempt) printf("\n incorrect password\n")
			else {printf("Your card is confiscated\n"); exit(1);}
		}
	}

Thank you :)

Recommended Answers

All 4 Replies

What happens after the third attempt?

>>(che = getch()) != EOF
EOF is only generated on MS-Windows by pressing Ctrl+Z key combination. How likely is that ever to happen in your program?

>>&& i < sizeof(pword)
variable i needs to be reset back to 0 before second and third attempts to erase the previous password so that the user can enter a new password. I'd put i = 0; on line 7, just before that while statement.

Hi, Ancient Dragon,

after the third attempt the exit from program happens.

I put i=0 before while and it works. Simple thing which i cannot get :))

Honestly, i found the code for the hidden password input by asterisks, because I've tried to make it with GETPASS() function, but it is not recognized in my Dev++ compilator, so i decided use other way.I am not so good at programming, and i really thank you for your help :)

Hi, again, i want to ask one question, i have piece of code where i am trying to validate user account name input, and it seems working, despite of one thing if I put non existing name, the programs continue work, however it should be terminated. The code of problem is here:

printf("\nEnter Account Name: ");
    fflush(stdin);
    fgets(name, sizeof(name), stdin);        
    int len = strlen(name);
    if(name[len-1] == '\n')        
    name[len-1] = '\0'; 
    while (fread(buffer, sizeof(account), 1, members))
	{ 
		if(strcmp(buffer->acc_name, name)==0){
               fgetpos(members,&here);
               printf ("\nAccept! Account exists.");
               break;
               fsetpos(members, &here);
        } 
        else{
              if (!feof(members))continue;
              else 
             {
               break; printf("Account does not exist");
               exit(1);}
              } 
        
	}

when i debugging the code with non exisistng account name, it missing this part, and continues going through the program,

else 
             {
               break; printf("Account does not exist");
               exit(1);}
              }

And i really cannot understand why it doesn't go there.I've tried to change the loop structure, but it doesn't work. Maybe again it is simple think, but i really don't have idea what it can be. Thank you very much :)

I found the way make it work :D

printf("\nEnter Account Name: ");
    fflush(stdin);
    fgets(name, sizeof(name), stdin);        
    int len = strlen(name);
    if(name[len-1] == '\n')        
    name[len-1] = '\0'; 
    j=0;  
	while (fread(buffer, sizeof(account), 1, members))
	{ 
		if(strcmp(buffer->acc_name, name)==0){
               fgetpos(members,&here);
               printf ("\nAccept! Account exists.");
               ++j;
               break;
               
               fsetpos(members, &here);
        } 
        else
            {  if (!feof(members))continue; ++j;}
            
        	}
	if (j<=0) {
                 printf("Account doesn't exist");
                 exit(1);
                 }

Maybe it doesn't look professionally, but for now it works :D If someone will see mistake, please, you are welcome !!!
Again thanks to Ancient Dragon :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.