Thread: New to C. Confused over loop behaviour

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    New to C. Confused over loop behaviour

    Hi,
    I just started learning C. In fact im not even through the entire tutorial yet. I've got very little experience with programming but i get the basic idea of it.

    I wrote a small program just to get used to the syntax and practice a bit. It's suposed to be a kind of an electronic bank account simulation program.

    Anyway, this loop im confused over is supose to take the users password, check whether its correct or not. If its not correct its suposed to give you 2 more tries before giving you the message that you've extended your limit of tries.
    However, it only gives you one more try before that happends.

    Here is the code:

    Code:
    int main()
    {
        int A;
        printf("Welcome to SeaBank electronic bank service. Seabank, Syncing people with finance.\n At any time you would like to go back through the menu, please press 6\n");
        printf("Please enter your four digit password.\n");
        scanf("%d",&A);
    
    
        if (A != password)
        {
            int x;
            for (x = 0;(A != password);x++)
            {
                printf("Your password is incorrect. Please try again.\n");
                getchar();
                scanf("%d",&A);
                if (x = 3)
                {
                    printf("You have typed in the wrong password too many times. Please consult your bank for further inquiry");
                    break;
                }
            }
        }
        else {account();}
        return;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What type of variable is the password?

    Also the getchar seems to be not nesecsairy ( I will never learn the spelling of this word! ).

    I would also suggest you to use a do while loop, because the loop is going to be executed once, no matter what
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    if (x = 3)
    = is for assignments, == is for comparisons

    In your code you assign 3 to "x" and then you check if the whole expression is true or false. In your case it's true because the result of the assignment is 3 and 3 is interpreted as true in C.

    As always, turning on compiler warnings would have told you.

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    you need to break out of the for loop if the password is right, if not continue for loop...

    BEST way would be the do...while loop, that breaks out if it is right

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Code:
    int main()
    {
        int A = 0;//Your variable needs to be initialized, always. If you don't set it to 0, it will be whatever value is stored in the memory A is currently using (unless your compiler auto-initializes.)
        int password = 1234;//You need to define your password!
        int garbage;//garbage scanned in at the end of the program so it doesn't close automatically
        int x;//This should be as far up as possible so as not to interfere with your later code.
        printf("Welcome to SeaBank electronic bank service. Seabank, Syncing people with finance.\n At any time you would like to go back through the menu, please press 6\n");
        printf("Please enter your four digit password.\n");
        //this isn't necessary: scanf("%d",&A);
     
     
        if (A != password)
        {
            
            for (x = 0;(A != password);x++)
            {
    
                //This isn't necessary: getchar();
                scanf("%d",&A);
                if(A == password)break;//You need this line to break out of the loop if the user enters the right value.
                printf("Your password is incorrect. Please try again.\n");
                if (x == 3) //As AndiPersti has correctly noted, == is a conditional whereas = is an assignment.
                {
                    printf("You have typed in the wrong password too many times. Please consult your bank for further inquiry");
                    break;
                }
            }
        }
        else {}//account(); has not been written yet or is not included in your sample code
        scanf("%d",&garbage);//line to scan in a value so the program doesn't close at the end automatically. My compiler closes so I need this to keep it open
        return;
    }

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    Thank you very much for your answers!

    By the way, how would i go about to turn on compiler warnings?

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    @Derek
    Oh sorry.I have written more code that i left that out when i posted (for example the account() function) because it was only really the loop i was confused about. Perhaps I should include the whole program next time.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Ah, I figured.
    That's ok, just so you know the code I put up compiles and works logically. Now you just need to see my comments and write your account() function.
    By the way, I wouldn't put your account function in the else statement where you had it. If you enter the correct password, it does not check again if the password is correct, it just skips past the else.
    Last edited by Derek Lake; 01-14-2013 at 04:47 PM. Reason: accidentally a word

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Joner View Post
    By the way, how would i go about to turn on compiler warnings?
    Tell us which compiler you use and someone here probably knows.

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    scanf("%d",&garbage);//line to scan in a value so the program doesn't close at the end automatically. My compiler closes so I need this to keep it open
    Why not a simple getchar()?
    And it's not the compiler which closes the terminal window because it has nothing to do with program execution. It's either your IDE (the compiler is only a part of it) or your OS.

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    5
    I use codeblocks

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code::Blocks is an IDE not a compiler.

    I don't know it but looking at the docs, you can set the compiler flags at "Settings/Compiler and Debugger/Other Settings". The image shows the available flags for the gcc compiler. If you use another one, the flags may be different.

    For gcc I recommend at least -Wall ("Enable all compiler warnings").

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour
    By cfanatic in forum C Programming
    Replies: 2
    Last Post: 07-16-2012, 06:41 AM
  2. Confused: while loop and characters
    By DucksgoQuack in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:24 PM
  3. Confused by behaviour
    By rocketman50 in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2010, 10:55 AM
  4. Replies: 10
    Last Post: 12-16-2008, 10:28 AM
  5. confused by Do While loop
    By elton_fan in forum C Programming
    Replies: 1
    Last Post: 11-01-2007, 10:25 AM

Tags for this Thread