gpt4 book ai didi

c - C语言迭代中的while循环

转载 作者:行者123 更新时间:2023-11-30 20:35:40 25 4
gpt4 key购买 nike

我不知道我的错误在哪里,请帮帮我。当我在用户名和密码中输入错误的数据时,它会询问"is"或“否”以重试。如果我输入 Y 作为"is",我将无法再输入用户名。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main() {
printf ("\n\t\t Welcome To Your Payroll Account!");
printf ("\n\t\t Please Enter Your Username & Password To 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: ");

for (i = 0; i<8; i++){
ch = getch();
password[i] = ch;
printf ("*");
}

password[i] = 0;
printf("\n Your password is: ");

for (i =0; i<8; i++) {
printf("%c", password[i]);
}

if(strcmp(pass,password) == 0 && strcmp(name,username) == 0) {
printf("\n \n You are logged in! \n");
printf("Enter # to proceed \n");
scanf("%c", &input);
} else {
printf("\n \n Incorrect username or password");
printf("\n\n Do you want to 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;
}

在这里,在获取字符输入时,您正在使用 scanf()

so将给出字符输入加回车。再次将获取输入字符,但输入将保留在缓冲区中。

当while循环迭代时,等待用户名输入的gets会自动从缓冲区中获取输入字符,因此用户名输入将被跳过。

解决办法是在后面添加getch()

scanf("%c", &again);

希望这对您有帮助。

关于c - C语言迭代中的while循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38636178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com