[英]Password prompt
我正在嘗試編寫一個簡單的密碼提示。.我必須承認,我找到了一些代碼,但是對於我的特定目的它不起作用。 我仍在學習C,這只是階梯式的另一步。 我試圖將其恢復到密碼正確的地方,如果密碼不正確,它將終止。
int c;
char pass[20] = "";
char *end = pass + sizeof(pass) - 1;
char *dst = pass;
printf("Enter password: ");
while ((c = getchar()) != EOF && c != '\n' && dst < end)
*dst++ = c;
*dst = '\0'; // Ensure null termination
printf("\nPass: %s\n", pass);
return 0;
該代碼似乎在“輸入密碼”提示下“鎖定”。 謝謝你的幫助。 非常感謝。
您需要使用帶strcmp
的if
語句來檢查輸入的密碼是否正確。
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
int c;
char pass[20] = "";
char *end = pass + sizeof(pass) - 1;
char *dst = pass;
char admin[] = "12345";
printf("Enter password: ");
while ((c = getchar()) != EOF && c != '\n' && dst < end)
*dst++ = c;
*dst = '\0'; // Ensure null termination
if(strcmp(admin, pass) == 0)
printf("\nPass: %s\n", pass);
else
{
printf("Incorrect password");
return 0;
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.