pin number question

This is a discussion on pin number question within the C Programming forums, part of the General Programming Boards category; This question has been posted here before, but I need a slightly more detailed answer. So here goes. I am ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    18

    pin number question

    This question has been posted here before, but I need a slightly more detailed answer. So here goes. I am working on a project that is, basically, an atm simulation. The part I'm having problems with (at the moment) is the PIN number. For this exercise, the pin has to be any 4 digit number. Meaning, the program won't be looking for a specific number, but rather any number with 4 digits. Any suggestions on how to do this?

    melee

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Can the pin number begin with zeros (i.e. 0001)? If yes, you'd probably want to read it as a string.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    yes, the pin can begin with a zero. But how does one read it as a string and then verify that it does indeed have 4 digits? (sorry for the newbie questions)
    melee

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Read in the number using a char array - use fgets() reading stdin -- all defined in stdio.h
    Check that each character of the string is a number - use isdigit() from ctype.h
    Check that the length of the input string is exactly four chars - strlen() from string.h

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    If you read it in as a string, use strlen() to find out how many characters you have, then isdigit() to verify each is a number, you can then be sure that atoi() will give you a valid integer.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Ok, sorry It took me so long to get to this, but I'm still having problems. Part of my confusion lies in the fact that we have not covered the strlen or fgets functions in my class as of yet. I put the relevant code here so someone wiser can point me in the right direction.

    Code:
    int main(void)
    {
        /*Local Definitions*/
        int    count = 0;
        char    pin[4];
        float  bal = 1000;    
        int    dep;
        int    with;
        int    pinlen;
        int    mainchoice;
        int    check = 0;
            
            
            
        /*Statements*/
        
        printf("\tWelcome to the COP2220 ATM");
        printf("\n\t Please enter your 4 digit PIN \n ");
          scanf("%c", &pin);
                if (strlen(*pin) = 4)
    I didn't post any of the other code because (1.) This is the point at which I am stuck. The rest of my code surely contains numerous errors, but I'll do my best to figure those out on my own. and (2.) This is where my compiler stops with an error (invalid conversion from "char"to "const char")

    any help?

    melee
    Last edited by melee; 12-10-2004 at 09:42 PM. Reason: included a bit more code

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if (strlen(*pin) = 4)
    1) You don't ever dereference arrays.
    2) Just use the name of the array, which will be in effect a pointer to its first element.
    3) You use == for equality. = for assignment.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Aha...problem solved. Let's see if I can go a few minutes without causing another. Thanks!
    melee

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    Ok, I'm a bit farther (at least syntactical error-wise). Here's my updated code. Currently I'm stuck on the balinq function. Any help would be greatly appreciated. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    /*Prototype Declarations*/
    float fastcash   (float bal, float fwith);
    float Withdraw   (float bal, float with);
    float Deposit    (float bal, float dep);
    void  balinq     (float bal);
    void  endtrans   (void);
    int   mainchoice (void);
    
    
    
    
    
    
    int main(void)
    {
        /*Local Definitions*/
        int     count = 0;
        char    pin[4];
        float   bal = 1000;    
        int     dep;
        int     with;
        int     choice;
        int     check = 0;
          
            
            
        /*Statements*/
        
        printf("\tWelcome to the COP2220 ATM");
        printf("\n\t Please enter your 4 digit PIN \n ");
          scanf("%c", &pin);
          
                
                if (strlen(pin) != 4)
                      printf("\a\nThat PIN is incorrect. Please re-enter your 4 digit PIN \n");
                      ++count;
                if (count == 3)
                      void endtrans (void);    
                              
        
                int mainchoice (choice);
                 if (mainchoice == 1)
                    float fastcash  (float bal);
                 else if (mainchoice == 2)
                    float withdraw  (float bal);
                 else if (mainchoice == 3)      
                    float deposit   (float bal);
                 else if (mainchoice == 4)                            
                    float balinq    (void);
                 else
                    void endtrans   (void);
                 return 0;
      }    
          
          /*==================================Mainchoice===========================
                This function asks the user to select which type of transaction he/she wants
                to make.
          */
          int mainchoice (void)
    {
          /*Local Definitions*/
          int choice;
          
          /*Statements*/
          printf("\n\tPlease make a selection from the list below and hit Return:");
          
          
          
          printf("\n\t*****************************");
          printf("\n\t*           MENU            *");
          printf("\n\t*                           *");
          printf("\n\t*   1. Fast Cash            *");
          printf("\n\t*   2. Withdrawal           *");
          printf("\n\t*   3. Deposit              *");      
          printf("\n\t*   4. Balance Inquiry      *");           
          printf("\n\t*   5. End Transaction      *"); 
          printf("\n\t*                           *");
          printf("\n\t*****************************");
          
          scanf("%d", &choice);
          return choice;
    }     /*mainchoice*/    
          
                                                      
                                                                            
       
            
          /*============================Fast Cash================================
                Asks the user to select from a list of withdrawal amounts.  The 
                amount is then subtracted from the balance.
          */
          
          float FastCash (float bal,
                          int fwith)
          
          
      {    /*Statements*/
          
          printf("\n\tPlease select the amount of money you would like to withdraw:");
          
          switch (fwith)
                {
                      case 1:  printf("$20.00\n");
                               break;
                      case 2:  printf("$40.00\n");
                               break;
                      case 3:  printf("$80.00\n");
                               break;
                      case 4:  printf("$100.00\n");
                               break;
                      case 5:  printf("Main Menu\n");
                               break;  
                }    
                bal = bal - fwith;
                
    }/*fastcash*/            
                
                
                
                
          /*============================Withdrawal================================
                Asks the user to enter the amount of money to be withdrawn.  The amount
                is then subtracted from the balance.
          */
          float Withdraw (int with,
                          float bal)
          {
           /*Statements*/   
                 printf("Please enter the amount of money you would like to withdraw: \n");
                 scanf("%f", &with);
                 bal = bal - with;
                 return bal;
      }    /*Withdrawal*/
      
      
      
      
      
          
          /*==============================Deposit==================================
                Asks the user to enter the amount of money to be deposited.  The amount
                is then added to the balance.
          */
          float Deposit (float bal,
                         float dep)
          {
            /*Statements*/  
                  printf("Please enter the amount of money you would like to deposit: \n");
                  scanf("%f", &dep);
                  bal = bal + dep;
                  return bal;
         }  /*Deposit*/
          
          
          
          
          
          
          /*==============================BalInq============================
                Prints the users current balance.
          */
          
          void balinq (bal)                       /*<---error occurs here*/
          {
              /*Statements*/
                    printf("\n\tYour current balance is: %f", *bal);
                    return 0;
          }/*BalInq*/
      
      /*==================================endtrans===================================
                This function ends the transaction either at the request of the user or 
                when the incorrect PIN is entered too many times.
          */
          void endtrans (void);
          {
          /*Statements*/
                printf("\n\t Goodbye!");
                      return;
          }/*endtrans*/    
        
      
      
      system("PAUSE");	
      return 0;
    }

    the error I'm getting on the balinq function is " "bal" was not declared in this scope"

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You should probably specify bal's type in your function prototype.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf("\n\tYour current balance is: %f", *bal);
    And to print out bal you don't need to dereference it:
    printf("\n\tYour current balance is: %f", bal);

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    18
    ahem...

    DOH!!
    melee

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    And, if, as the function declaration states, bal should be a float, then don't dereference it. Only dereference pointers.

    *edit*
    Nevermind...
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  15. #15
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    bal = bal - fwith
    This is perfectly correct, but just a cool trick:
    Code:
    bal -= fwith
    Does the exact same thing. You'll also see the same thing with +, *, and /.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 02:31 AM
  4. help with number menu
    By j4k50n in forum C Programming
    Replies: 12
    Last Post: 04-17-2007, 02:33 AM
  5. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 01:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21