سؤال

#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;
}

The above is my code. The objective is to be able to enter a normal 5 digit pin such as 12345. The program will then store that pin, and produce a screen like this:

Pin: 0 1 2 3 4 5 6 7 8 9 Num: 3 1 2 3 2 2 3 1 1 3

The program randomized the num part that you see and assigns it to the numbers above. That way the user reenters 12322 and the computer recognizes it as him entering the right code. The next time it where to do it, it would be rerandomized to something else. This whole project is designed to prevent shoulder surfing.

However I seem to be having a problem with my code. Almost everything is working but with the final strcmp function. Even though I have pinVerify set to 5, it still gives me 5 numbers and a ASCII character at the end, something like 21323☻. This makes it impossible to ever have pinVerify and pinReenter as equal, meaning it's impossible for the program to tell you you entered in the challenge correctly, even when you did.

Can anyone help me fix this? I've been looking and tinkering for a while and have been completely unsuccessful.

هل كانت مفيدة؟

المحلول

C-style strings are null-terminated. In your example, it means that, when you want to work with two strings of length 5, you should actually reserve 5+1=6 chars for each of them, and make sure that the 5-th (0-indexed) char contains a character with ASCII code 0. Otherwise, strcmp, and any other C-string function, proceeds past the end of your char[5] array until it finds a byte containing zero.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top