嗯,我有这个程序检查密码。如果我将第二个数组(即for循环)设置为8位数,它工作正常。但是一旦pw需要超过8位数字,整个事情就会出错(因为for循环会持续10位数字)。在C中,一个商店如何长字符串(例如密码)
我认为声明第一个数组是长时间MAXLINE会工作,但它似乎并没有解决问题。
/* IMPORT ---------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* CONST------------------ */
#define MAXDIGIT 10000
/* VARIABLES (global) ---------- */
/* MAIN--------------- */
int main()
{
/* VARIABLES (local) --------- */
/* VARIABLES (local) --------- */
// ENTERED PW:
char EnterCode[MAXDIGIT];
int i;
// REAL PW:
char arr[MAXDIGIT] = "123456789"; //"YKJ98LGDDF";
int j;
printf("PW: "); // for testing
for (j = 0 ; j < 8; ++j){
printf("%c", arr[j]);
}
/* Intro --------------------- */
printf("\nPlease enter code of authorization: ");
for(i = 0; i < 10; ++i){
scanf("%c", &EnterCode[i]);
printf("%c", EnterCode[i]); // test 1
}
if (strcmp(EnterCode,arr) == 0){
printf("\nAccess authorized.\n");
}else{
printf("\nAccess denied!\n");
}
system("PAUSE");
return 0;
}
你永远不会在'EnterCode'中插入''\ 0''字符。 'strcmp'要求字符串被正确终止。 – user694733 2014-12-02 13:41:21
读取输入,直到遇到新行字符... – Lundin 2014-12-02 13:41:48
'EnterCode'最有可能**不是** null **终止 – 2014-12-02 13:42:04