Thread: My pincode is having an error, it was supposed to have a 3 attempts.

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    My pincode is having an error, it was supposed to have a 3 attempts.

    So I am a 1st year computer engineer student, im having a problem with my loop my loop is supposed to look like this please send help My pincode is having an error, it was supposed to have a 3 attempts.-prugram1-jpg


    Code:
    //PROGRAMMER: EUSEBIO, ANGELO J.
    // CpE 1-B
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        int choice, deposit_amount, withdraw_amount, pin, j, i, trial;
        float initial_amount = 20000, current_amount;
        char width, trans;
        pin = 012345;
        printf("#######################################################################################################################");
        printf("\n#                                                                                                                     #");
        printf("\n#                                               WELCOME TO LANDBANK                                                   #");
        printf("\n#     ATM 24 - HOUR SERVICES                                                                 VISA|BANCHET|PAYMAYA     #");
        printf("\n#######################################################################################################################");    
        
        
        while (pin = 12345)
        {
            for(j=1; j<=3; j++)
            
                {
                printf("\n");
                printf("%45c ENTER YOUR SECRET PIN NUMBER: ", width);
                scanf("%d", &pin);
            
                if (pin == 012345)
                {
                    goto besugo;
                    break;
                }
                else 
                {
                j++;
                printf("%45c PLEASE ENTER A CORRECT PIN NUMBER %d trial left",width, j);
                j--;
                }
                
        }
        besugo:
        printf("\n\n%37c*************Welcome to ATM Service*************", width);
        printf("\n%49c 1. Balance Inquiry ", width);
        printf("\n%50c 2. Withdraw Cash ", width);
        printf("\n%50c 3. Deposit Cash ", width);
        printf("\n%50c 4. Quit ", width);
        printf("\n%37c************************************************", width);
        printf("\n\n%45c Enter your choice: ", width);
        scanf("%d", &choice);
        
        switch(choice)
        {    
            case 1:
            printf("%45c YOUR BALANCE IS: %.0f", width ,initial_amount);
            whodoes:
            printf("\n%45c Do you wish to have another transaction?(Y/N): ",width);
            scanf("%c", &trans);    
            scanf("%c", &trans);
            
                switch (trans)
                {    
                    case 'Y':     
                    goto besugo;
                    break;    
                        
                    case 'y':     
                    goto besugo;
                    break;    
                                
                    case 'N':    
                    printf("\n\n%45c THANK YOU FOR USING ATM SERVICE!", width);
                    break;
                    
                    case 'n':    
                    printf("\n\n%45c THANK YOU FOR USING ATM SERVICE!", width);
                    break; 
                    }    
                    {
                    break;
                    }
            case 2:    
            printf("%45c ENTER AMOUNT TO WITHDRAW: ",width);
            scanf("%d", &withdraw_amount);
            current_amount = initial_amount - withdraw_amount;
            
                if (withdraw_amount>10000)
                {
                printf("\n%45c You can only withdraw maximum amount of 10,000.00", width);
                goto whodoes;
                }
                else
                {
                printf("\n%45c PLEASE COLLECT CASH.......", width);     
                printf("\n%45c YOUR CURRENT BALANCE IS %.0f", width, current_amount);
                goto whodoes;
                break;
                }
                {
                break;
                }
            case 3:
            printf("%45c ENTER THE AMOUNT TO DEPOSIT: ", width);
            scanf("%d", &deposit_amount);
            current_amount = initial_amount + deposit_amount;
            printf("%45c YOUR CURRENT BALANCE IS %.0f", width, current_amount);
            goto whodoes;
            break;
            {
            break;
            }
            
            case 4:
            printf("\n\n%45c THANK YOU FOR USING ATM SERVICE!", width);
            break;
            
            default:
            printf("\n\n%45cINVALID TRANSACTION!", width); 
            break;            
    }
    break;
    }
        
        
        
        
        
        
        
    getch();
    return 0;    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have a few recommendations:
    • Replace your use of goto + label with function call + function. This will make it easier to trace through your code and reason about it.
    • More generally, break your main function into smaller functions that each does one thing and does it well. This will make it easier to reason about your code because you can reduce the amount of state that you have to worry about to mainly local state, and with less to think about, you're more likely to think along the right lines.
    • Indent your code more consistently. This will also make it easier to trace through your code to figure out the flow of control.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2021
    Posts
    5
    Thanks a lot i will try all your recommendations, thank you for replying. Have a good day!

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please learn and remember the difference between "==" and "=".

    Code:
    while (pin = 12345)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Feb 2021
    Posts
    5
    ya my bad, "=" means asigning value right? while "==" for operator

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > pin = 012345;
    You should be aware that when you write numbers in C that begin with 0, you're saying they are octal (base 8), not decimal (base 10) numbers.

    Code:
    // Example program
    #include <stdio.h>
    
    int main()
    {
      int a = 12345;
      int b = 012345;
      if ( a == b ) {
          printf("Yes\n");
      } else {
          printf("No!!!\n");
      }
    }
    C++ Shell
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Also, PINs (also IDs, SSNs, phone numbers, etc.) should not be treated as numbers, even though they are numeric. They should be strings. Your example gives a good reason: if the user enters something like 0000000012345, it'll match the PIN 12345, if you input the PIN as a number. If you input the PIN as a string instead, only the correct PIN will match.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-26-2016, 07:09 AM
  2. Password check with 3 attempts (split)
    By Shedrack Ayaro in forum C++ Programming
    Replies: 1
    Last Post: 02-24-2015, 01:15 PM
  3. Password check with 3 attempts
    By samwillc in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2012, 03:54 AM
  4. Error with function that attempts to fill an array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2011, 10:07 PM
  5. Reporting SSH break-in attempts
    By _Mike in forum Tech Board
    Replies: 7
    Last Post: 03-25-2010, 01:22 PM

Tags for this Thread