Search code examples
cfunctionterminate

I need a function that ask the user to enter a pin and after 3 wrong attempts, they program terminates


I have to write an ATM program for a class, and i cant figure out how to make a function that will ask the user for a pin and if the pin is entered incorrectly three times the program will display an exit message then terminate.... this is what i have some far. I think my issue is i don't know the correct syntax to handle my issue.

I know i will need a for loop but not sure how exactly to construct it.

void validate_acc(){
     int user_acc_try;

     printf("Please enter your account number: ");
     scanf("%d", &user_acc_try);

     if(user_acc_try != account_number){
                     printf("You entered the wrong account number");
                     }
     else{
          printf("");
          }
}

void validate_pin(){
     int user_pin_try;

     printf("Please enter your pin number: ");
     scanf("%d", &user_pin_try);

     if(user_pin_try != pin){
                     printf("You entered the wrong pin number.");
                     }
     else{
          printf("");
          }
}

void validate(){
     validate_acc();
     validate_pin();
}

Secondly, since i can only post every 90 minutes might as well ask another question, I do not know how to make a function go back to the beginning of my program like for example say after an deposit, what is the logic i would need to use to have a function go back to the beginning of my main function. I know of goto labels, that didnt seem to work when i put it in front of my main function like so...

MAIN:
int main()

i would put goto main; in another function and i would get a.... Main is not defined error. I have read a few different questions on here about labels but cant find anything that helps, if someone could guide me in the right direction, you would be giving me a great deal of relief.

thank you in advance.


Solution

  • It's a good idea to write out a flow chart for things like this if you can't figure out how to do it in code.

    Please do not use labels/goto in C. It's a nasty habit and it's not needed.

    You know how to use if statements to make a decision; think about how you would use a while loop to try to make the same decision over and over again until something changes. For instance, in pseudo-code (because I don't want to do your work for you)

    user_has_not_entered_correct_pin = true
    retries_left = 3
    while retries_left > 0 and user_has_not_entered_correct_pin:
    get pin
    if pin_is_not_correct(pin) retries = retries - 1
    else user_has_not_entered_correct_pin = false
    end while