1

Homework Assignment #2 for Program Design Class

Part 1:

  • Prompt user for 8 digit account number (0-9), repeat until valid.

Part 2:

  • Prompt user to set 4 digit pin number (0-9), repeat until valid.
  • Prompt user to verify pin number, return to Part 2 if invalid.

The program works, however, I'd like to validate for some extra things:

  1. Accept leading zeros ex., '00123456'
  2. Reject additional letters ex., '12345678a'
  3. Reject additional 'words' ex., '12345678 123abc'

I'm thinking, prompt for a string input, check the length of it (4 or 8) and if it passes that test, convert it to an integer and proceed with the tests in place.

Any thoughts?

[ A lot of you dislike the use of scanf, I know. I'm more interested in how I can make minimal changes to my program instead of reinventing [my] wheel! :) ]

#include <stdio.h>

int main()
{

    int return_val = 0;         
    int account_number = 0;
    int pin_number = 0;
    int pin_number_verify = 0;
    int valid_pin = 0;


    // Account # Validation
    while(1)
    {
        printf ("Please enter your 8 digit account number:\n");
        return_val = scanf("%d", &account_number);

        if((account_number > 9999999) && (account_number < 99999999))
        {
            if (return_val == 1)
            {
                break;
            }
        }

        printf("Invalid account number. Account number must be 8 digits.\n\n");

        while (getchar() != '\n');  /* Clear keyboard input buffer */
    }

    return_val = 0;


    // Pin # Validation
    while(1)        
    {
        printf ("\nPlease choose a 4 digit pin number:\n");
        return_val = scanf("%d", &pin_number);

        while (getchar() != '\n');  /* Clear keyboard input buffer */

        if((pin_number > 999) && (pin_number < 9999))
        {
            if (return_val == 1)
            {
                while(1)
                {
                    printf("Re-enter pin number:\n");
                    return_val = scanf("%d", &pin_number_verify);
                    while (getchar() != '\n');  /* Clear keyboard input buffer */

                    if(pin_number != pin_number_verify)
                    {
                        printf("Pin setup unsuccessful\n\n");
                        break;
                    }
                    else
                    {
                        valid_pin = 1;
                        break;
                    }
                }
            }
        }

        if (valid_pin == 1) {
            break;
        }

        printf("Invalid pin number. Pin number must be 4 digits.\n");

        while (getchar() != '\n');  /* Clear keyboard input buffer */
    }


    // Successful account setup prompt
    printf("\nPin setup successful!\n");
    printf("Account #: %d\n", account_number);
    printf("Pin #: %d\n", pin_number);
    printf("Have a nice day.\n");

    return 0;
}
Haris
  • 12,120
  • 6
  • 43
  • 70
Jesse Walton
  • 183
  • 1
  • 3
  • 11
  • Does the program work or not? Have you run test cases to see if the proper things are accepted/rejected? – John May 31 '13 at 16:27
  • Yes, it works. It just validates sometimes when it shouldn't and vice versa. Edit: when I say 'shouldn't' I mean based on my desired results. – Jesse Walton May 31 '13 at 16:29
  • Using `strtol()` might not require much change and would be a lot easier, but you may already know that based on your note above. :) – Dan Fego May 31 '13 at 16:34
  • I've never used it before but I'll look into it! – Jesse Walton May 31 '13 at 16:38
  • possible duplicate of [Restrict user input to 8 digit number](http://stackoverflow.com/questions/16848822/restrict-user-input-to-8-digit-number), except it asks about 4 digits instead. – Ken White May 31 '13 at 18:24

3 Answers3

0

I have done a similar approach by having a char array, then used a function that accept the right amount of digit, which can include alphabets or special signs, and used a validating function to validate the input all a series of digits to pass validation. all depending on the requirements!

you also can use a function that read all digits of the right amount and save each digit into the char array and increment a pointer, if you receive any invalid input while typing, you can ignore it and keep taking input for up to a number of ignoring time till breaking out, or return an error.

after all I think you should use a char array because 0001 is still a valid pass key.

aah134
  • 860
  • 12
  • 25
0

If you want to check if they've entered the right number of characters, read a line of input and check the length. Then you can check the contents for validity (all numeric).

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
0

you can use isalpha() to check there is any character or not.