首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pin挑战响应系统

Pin挑战响应系统
EN

Stack Overflow用户
提问于 2014-04-29 20:42:05
回答 1查看 447关注 0票数 0
代码语言:javascript
复制
#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 Num: 3 1 2 3 2 2 3 1 1 3

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

但是,我的代码似乎有问题。除了最终的strcmp函数外,几乎所有东西都正常工作。尽管我将pinVerify设置为5,但它仍然给出了5个数字和末尾的一个ASCII字符,大约是21323☻。这使得不可能让pinVerify和pinReenter相等,这意味着程序不可能告诉您您正确地参加了挑战,即使您参加了挑战。

有人能帮我解决这个问题吗?我一直在寻找和修补一段时间,并已完全失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-29 20:49:25

C样式字符串为空终止。在您的示例中,这意味着,当您想使用两个长度为5的字符串时,实际上应该为每个字符串保留5+1=6 chars,并确保第5( 0 -索引) char包含一个具有ASCII代码0的字符。否则,strcmp和任何其他C-string函数都会继续到char[5]数组的末尾,直到它找到包含零的字节为止。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23374851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档