Can someone help me to write a code in C Programming that will ask the user to enter a PIN and if the PIN is entered 3 times wrongly the program should exit.

Thanx in advance.......

Why don't you try creating a variable for the PIN and comparing it to the user input. You should also have a variable that keeps track of the incorrect attempts, and if it equals three, exit the program. Post your attempt.

I'm new in programming and below is the code that I've attempted.........
Where did I go wrong?

#include<stdio.h>
#include<stdlib.h>
main ()
{
    int pin=1234;
    int i, a=0;


    printf("Enter pin.\n"); //The user is prompt to enter a PIN
    scanf("%d", &i);        // The user inputs the PIN

    {
        if(i==pin)
            printf("The PIN is correct.\n");

        else
            {
                printf("Invalid PIN.\n");   
        while(a!=3)             // If PIN is entered three times wrongly it'll exit
            {
                printf("Re-enter the PIN.\n");
                scanf("%d",&i);
                a++;
            }
        printf("You'll be loged out!\n");   
        exit(1);
            }
    }

return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Use code tag's in your post to make it easer to read:
[code=C]
Code here.
[/code]

You have no int infront of your main(). :P

Look at this:

if(i==pin)    //First time, the input is checked
  printf("The PIN is correct.\n");

else
{
  printf("Invalid PIN.\n");
  while(a!=3) //Is it being checked in this loop?
  {
    printf("Re-enter the PIN.\n");
    scanf("%d",&i);
    a++;
  }
printf("You'll be loged out!\n");
exit(1);

So, if your first pin was wrong, the rest will never be tested.

1. Use code tags!
2. The biggest problem you have (though not the only one) is that once you have entered the while loop, there is no way out, because there is no way to make sure you have the right PIN. It will always get to (a == 3) and exit the program.

3. The program is not counting the first incorrect entry of the PIN. Do what you can to fix that error.

4. Lastly, the exit function usually doesn't give the user enough time to read the error, and is not necessary because the program will finish execution at that point anyway.

Hi, partick k

I've recently started learning c programming, therefore it will be very helpful to help on the code that i've given please.

I really appreciate the responses that i got so far

Hi, partick k

I've recently started learning c programming, therefore it will be very helpful to help on the code that i've given please.

I really appreciate the responses that i got so far

Just add a check to see if the user input is equal to the key in the loop.
It might look like:

let pin = 1234
let count = 0
let in = 0

while count is less than 3 and in does not equal pin
  print "Enter pin: "
  input in
  add 1 to count

if in equals pin
  print "Correct"
else
  print "Fail"

Dear Hiroshe

I've tried your suggestion but does not run, just exits before it runs.
Can you please code it for me, please?

Thanx

Dear Hiroshe

I've tried your suggestion but does not run, just exits before it runs.
Can you please code it for me, please?

Thanx

Yes I can code it for you. But I am not. Glad I was able to answer your question.

If you where able to write the code at post #3, it should be easy to covert what I had written into C. If you didn't write the code at post #3, than you'll have to learn C to get the answer.

Dear Hiroshe

Thanx for the reply, finally I got it right. I followed your guiding and finally got it as I wanted it to work.

Thanx again and wish you all the best in your good work

This question has already been answered. Start a new discussion instead.