这段代码在没有 scanf 行的情况下产生正确的输出。该函数在使用 scanf 时返回不同的值。为什么会这样??
int main()
{
int choice;
scanf("%d",&choice);
bool a=admi_login();
if(a==true)
printf("\n hello");
else
printf("Bye");
}
bool admi_login()
{
printf("Enter the Admin Password\n");
char pw[100];
int i;
for(i=0;i<=100;i++)
{
pw[i]=getch();
if(pw[i]=='\r')
break;
printf("*");
}
char pw1[100]="admin\r";
if((strcmp(pw,pw1))==0)
return true;
else
return false;}
最佳答案
scanf 将在遇到的第一个空白处保留输入流。您的登录函数在第一个空格处停止,因此它永远不会读取密码的其余部分。
关于c - 当我从此代码中删除 scanf 行时,我得到正确的输出为 "hello"但是在使用 scanf 时我得到的输出为 "bye".... 为什么这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441863/