首页 文章

Pin Challenge Response Sysem

提问于
浏览
0
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string.h>

using namespace std;

int main()
{
char pin[6]; //character array to allow for ease of input
int randomNumbers[10]; //holding the randomized numbers that are printed back to the user
int pinStorage[5];
char pinVerify[5];
char pinReenter[6];
int result;

srand(time(0));

cout << "Enter your pin number" << endl;
cin >> pin;

for (int i = 0; i < 5; i++)
{
    pinStorage[i] = pin[i] - '0';
}


for (int i = 0; i < 10; i++)
{
    randomNumbers[i]= rand() % 3 + 1;
}

cout << "Pin Verify: ";

for (int b = 0; b < 5; b++)
{
    for (int d = 0; d <10; d++)
    {
        if (pinStorage[b]== d)
        {
            pinVerify[b] = randomNumbers[d];

            switch (pinVerify[b])
            {
                case 1: pinVerify[b] = '1';
                break;
                case 2: pinVerify[b] = '2';
                break;
                case 3: pinVerify[b] = '3';
            }
            cout << pinVerify[b] << " ";
        }
    }
}

cout << " " << endl;
cout << "Pin   : 0 1 2 3 4 5 6 7 8 9" << endl;
cout << "Number: ";


for (int c = 0; c < 10; c++)
{
    cout << randomNumbers[c] << " ";
}


cout << " " << endl;
cout << "Renter pin" << endl;
cin >> pinReenter;



for(int h = 0; h < 5; ++h)
{
    int digit = pinStorage[h];
    pinReenter[h] = randomNumbers[digit] + '0';
}

cout << "PV: " << pinVerify;
cout << "PR: " << pinReenter;


result = (strcmp(pinVerify, pinReenter));

switch(result)
{
    case 1: cout << "You did not enter the pin correctly!" << endl;
    break;
    case 0: cout << "Pin Entered Correctly!" << endl;
    break;
    case -1: cout << "You did not enter the pin correctly!" << endl;
}

以上是我的代码 . 目标是能够输入正常的5位数引脚,例如12345.程序将存储该引脚,并生成如下屏幕:

针:0 1 2 3 4 5 6 7 8 9数量:3 1 2 3 2 2 3 1 1 3

程序随机化你看到的num部分并将其分配给上面的数字 . 这样,用户重新进入12322并且计算机将其识别为他输入正确的代码 . 下次它在哪里做,它将被重新随机化为其他东西 . 整个项目旨在防止肩部冲浪 .

但是我的代码似乎有问题 . 几乎所有东西都在工作,但最终的strcmp功能 . 即使我将pinVerify设置为5,它仍然在最后给我5个数字和一个ASCII字符,类似于21323☻ . 这使得无法将pinVerify和pinReenter视为相等,这意味着程序无法正确地告诉您正确地进入了挑战,即使您这样做也是如此 .

谁能帮我解决这个问题?我一直在寻找和修补一段时间,并且完全没有成功 .

1 回答

  • 0

    C风格的字符串以空值终止 . 在你的例子中,这意味着,当你想使用两个长度为5的字符串时,你应该为每个字符串保留5 1 = 6 char s,并确保第5个(0索引) char 包含ASCII码为0的字符 . 否则, strcmp 和任何其他C字符串函数将继续超过 char[5] 数组的末尾,直到找到包含零的字节 .

相关问题