while looping in c languge iteration -
i don't know mistake, please need help. when enter wrong data in username , password , ask if yes or no try again. if type y yes can't type in username anymore.
#include <stdio.h> #include <stdlib.h> #include <conio.h> int main() { printf ("\n\t\t welcome payroll account!"); printf ("\n\t\t please enter username & password proceed."); printf ("\n\t ********************************************************* \n \n \n"); char password [10], username [10], ch; char name [10] = "username"; char pass[10] = "password"; char month, ename, address; float regpay,regov,rd,rdot,meal; float sss,ph,pi,union,other,ssssal,loan,hloan,uloan,eloan,td; float te,netpay; int n,day,year,i; char input; char again = 'y'; while (again == 'y' || again =='y') { printf("username: "); gets (username); printf("password: "); (i = 0; i<8; i++){ ch = getch(); password[i] = ch; printf ("*"); } password[i] = 0; printf("\n password is: "); (i =0; i<8; i++) { printf("%c", password[i]); } if(strcmp(pass,password) == 0 && strcmp(name,username) == 0) { printf("\n \n logged in! \n"); printf("enter # proceed \n"); scanf("%c", &input); } else { printf("\n \n incorrect username or password"); printf("\n\n want try again? yes(y) / no(n) \n"); printf("continue? "); scanf("%c", &again); again; if (again == 'n' || again == 'n') { system ("pause"); return 0; } else { again; } } } system ("pause"); return 0; }
printf("continue? "); scanf("%c", &again); again; if (again == 'n' || again == 'n') { system ("pause"); return 0; } else { again; }
here while taking char input using scanf()
so give character input plus enter. again input character, enter remains in buffer.
when while loop iterates, gets waiting username input enter character automatically buffer, username input skipped.
solution add getch() after
scanf("%c", &again);
hope hep you.
Comments
Post a Comment