Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 比较c++;_C++_Arrays_Comparison - Fatal编程技术网

C++ 比较c++;

C++ 比较c++;,c++,arrays,comparison,C++,Arrays,Comparison,我们有一个模拟atm功能的项目。用户必须输入一个pincode,它将被一个星号屏蔽。输入pincode必须等于存储在数组中的默认pincode。我的程序可以用星号屏蔽输入pincode,唯一的问题是,即使输入pincode与默认pincode相同,它仍然输出不正确。有什么问题吗?这是我的密码: void checkPword() { char defaultPin[4] = "1234"; char inputPin[4] = ""; clrscr(); for

我们有一个模拟atm功能的项目。用户必须输入一个pincode,它将被一个星号屏蔽。输入pincode必须等于存储在数组中的默认pincode。我的程序可以用星号屏蔽输入pincode,唯一的问题是,即使输入pincode与默认pincode相同,它仍然输出不正确。有什么问题吗?这是我的密码:

void checkPword()
{
    char defaultPin[4] = "1234";
    char inputPin[4] = "";

    clrscr();
    for (int cnt = 0; cnt <= 3; cnt++)
    {
        cout << "*";
        inputPin[ctr];
    }
    if (defaultPin[0] == inputPin[0] && defaultPin[1] == inputPin[1]
        && defaultPin[2] == inputPin[2] && defaultPin[3] == inputPin[3])
    {
        clrscr();
        cout << "pincode is correct"; 
    }
    else
    {
        clrscr();
        cout << "pincode is incorrect";
    }
}
void checkPword()
{
char defaultPin[4]=“1234”;
char inputPin[4]=“”;
clrsc();

对于(int cnt=0;cnt可能需要将getch()分配给ctr

内为

加上:说明

inputPin[ctr];
没有效果

您已添加:

inputPin[cnt] = putchar(ctr);
建议
为了让代码更清晰,将“cnt”替换为“i”

解决方案

char defaultPin[4]="1234";
char input[4] = "";
char currentChar;
bool pinFail = false;

for(int i=0; i != 3; i++) {
   currentChar = getchar();
   input[i] = currentChar;
   /* In this way you have only 3 if-control, not 3*4 as in your program */
   if(currentChar != defaultPin[i]) {
     pinFail = true;
   }
}

if(pinFail) {
   /* do something (print error?) */
} else {
   /* coutinue with your application */
}
void checkPword()
{
char defaultPin[4]={1,2,3,4};
char inputPin[4]=“”;
clrsc();

对于(int cnt=0;cnt)而言,文本字符串
“1234”
实际上包含五个字符。您不能忘记终止的
'\0'
。这意味着您的写入超出了
defaultPin
数组的限制。另外,语句
inputPin[ctr]是什么
应该做什么?您是否尝试添加一个断点以查看程序未完成预期操作的确切位置?调试的艺术不仅对该程序有用,而且对您编写的未来程序也有用。您没有将
getch()
分配给
inputPin
,是不是
inputPin[cnt]=getch();
char defaultPin[4]="1234";
char input[4] = "";
char currentChar;
bool pinFail = false;

for(int i=0; i != 3; i++) {
   currentChar = getchar();
   input[i] = currentChar;
   /* In this way you have only 3 if-control, not 3*4 as in your program */
   if(currentChar != defaultPin[i]) {
     pinFail = true;
   }
}

if(pinFail) {
   /* do something (print error?) */
} else {
   /* coutinue with your application */
}
void checkPword()
    {
    char defaultPin[4]={1,2,3,4};
    char inputPin[4]="";

    clrscr();
    for(int cnt=0;cnt<=3;cnt++)
        {
           inputPin[cnt] = getch();
           cout<<"*";
        }
        if ((defaultPin[0]==inputPin[0])&&(defaultPin[1]==inputPin[1])&&(defaultPin[2]==inputPin[2])&&(defaultPin[3]==inputPin[3]))
            {
             clrscr();
             cout<<"pincode is correct"; 
            }
        else
            {
             clrscr();
             cout<<"pincode is incorrect";
            }
    }