Thread: Another C question tonight

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    Another C question tonight

    I want to write a function that simulates an ATM operation.

    If a user incorrectly enters their pin more than 3 times, I want to show an error.

    This program is simple, so the only way the user can incorrectly enter a pin is if they enter less than four digits.

    How would I write this function?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    What have you tried?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Basically What I'm doing ruling out entries that are less than 1000. It's not working. Maybe it's the way I link int getPin to the main.. I'm not sure

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    //Global Dec's
    int getPin ();
    void getReciept (void);
    void menuChoose (void);
    
    int main (void)
    {
        //Local Dec's
        int attempts;
      
            
        printf("Welcome to Virtual Bank at West/n");
        attempts = getPin ();
        if (attempts <= 3)
        {
        getReciept();
        menuChoose();
    }
        else
        system("pause");
        return 0;
    }
        
    //getPin
    
    int getPin ()
    {
        int pin;
        int attempts;
        printf("Please enter pin number: \n");
    
         if (scanf("%d", &pin) < 1000)
            {
            attempts++;
            printf("Please re-enter pin number: \n");
            if (attempts > 3)
            printf("Sorry you can't continue, contact your bank for assistance.");
            }
         else
         return attempts;
    }//end getPin

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    hint: scanf doesn't return the number that it scanned

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    I don't follow, sl4nted

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And you need to loop with a loop of some sort. And you need to initialize uninitialized values before modifying/examining them.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by imfrrreal
    I don't follow, sl4nted
    you should read the decription of the scanf and make a special point on the return value of that function to use it properly
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    OMG I forgot to loop.

    Duhh

    Thank you!!!!

    I'll try fixing that and if I have another issue I'll post.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    if (scanf("%d", &pin) < 1000)
    I assume your test to see if its less than 3 digits here.

    try this

    add another variable int ret; it will hold scanfs return
    so
    ret = scanf("%d", &pin);
    printf("Returned: %d\n", ret);

    should show you whats going wrong

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Ok, check out int getPin now.. It's still not working properly.. after 2 attempts it moves on to getReciept in main.



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    //Global Dec's
    int getPin ();
    void getReciept (void);
    void menuChoose (void);
    
    int main (void)
    {
        //Local Dec's
        int attempts;
      
            
        printf("Welcome to Virtual Bank at West\n");
        attempts = getPin ();
        if (attempts < 3)
        {
        getReciept();
        menuChoose();
    }
        else
        system("pause");
        return 0;
    }
        
    //getPin
    
    int getPin ()
    {
        int pin;
        int attempts = 0;
        printf("Please enter pin number: \n");
        scanf("%d", &pin);
         while (pin < 1000)
            {
            attempts++;
              
            if (attempts < 3)
            {
            printf("Please re-enter your pin: \n");
            scanf("%d", &pin);
            } 
            else
            {
            printf("Sorry you can't continue, contact your bank for assistance.");
            return attempts;
             }
            system("pause");
            return attempts;
            }
    }//end getPin

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you problem is indentation - fix it and you will see the problem with the getPin function (when and what it returns...
    For example what it will return when 1001 is entered
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    nevermind
    Last edited by imfrrreal; 12-12-2006 at 02:36 AM.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I prefer to do error checking with a string myself. That way, input errors are independent of and easier to find than problems with scanf or magic numbers.

    Here's something that would work. I doubt it's bulletproof, and it should help you nonetheless:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getPin ( int *somepin )
    {
       char pinstr[6]; /* Just big enough I think */
       if ( fgets(pinstr, sizeof pinstr, stdin) != NULL )
       {
          char *tail = pinstr;
          while ( isdigit(*tail) )
          {
             tail++;
          }
    
          if ( tail - pinstr == 4 /* checking length */ && (*tail == '\0' || *tail == '\n') )
          {
             *somepin = atoi(pinstr);
             return 1; /* success!! */
          }
       }
    
       return 0; /* failure :( */
    }
    
    int main ( void )
    {
       int mypin, attempts = 0;
       while ( attempts < 3 )
       {
          printf("Enter PIN: ");
          fflush(stdout);
          if ( getPin(&mypin) )
          {
             attempts++;
          }
          else
          {
             printf("Not a valid entry.\n");
          }
       }
    
       return 0;
    }
    
    /* output:
    Enter PIN: q123
    Not a valid entry.
    Enter PIN:   12
    Not a valid entry.
    Enter PIN: aswd
    Not a valid entry.
    Enter PIN: 1234
    Enter PIN: 1234
    Enter PIN: 1001
    */
    It doesn't have to be difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM