1

We have a project that simulate the function of an atm. the user must enter a pincode and it will be masked with an asterisk. the input pincode must be equal to the default pincode that is stored in an array. My program can masked the input pincode with an asterisks, the only problem is that even if the input pincode is the same with the default pincode, it still output incorrect. what must be the problem? here is my code:

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";
    }
}
4
  • 2
    The literal string "1234" actually contains five characters. You can't forget the terminating '\0'. This means that you are writing beyond the limits of the defaultPin array. Commented Feb 14, 2014 at 8:47
  • 2
    Also, what is the statement inputPin[ctr]; supposed to do? Commented Feb 14, 2014 at 8:48
  • Did you try adding a breakpoint to see the exact point where the program does not do what you expect? The art of debugging is useful not only for this program, but for future programs you write. Commented Feb 14, 2014 at 8:52
  • You did not assign getch() to inputPin, is it that inputPin[cnt] = getch(); ? Commented Feb 14, 2014 at 8:55

2 Answers 2

1

Maybe you have to assign getch() to ctr?

ctr = getch();

Inside for..

PLUS: the instruction

inputPin[ctr];

does not have effects!

You have add:

inputPin[cnt] = putchar(ctr);

SUGGESTION
just to make code clear, replace "cnt" with "i".

SOLUTION

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 */
}
Sign up to request clarification or add additional context in comments.

Comments

0
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";
            }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.