Like Tree1Likes
  • 1 Post By Salem

terminating a loop with the '\n' key.

This is a discussion on terminating a loop with the '\n' key. within the C Programming forums, part of the General Programming Boards category; hey guys i have written a loop that stores numbers in an array and stops when the '\n' key is ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187

    Unhappy terminating a loop with the '\n' key.

    hey guys i have written a loop that stores numbers in an array and stops when the '\n' key is pressed but this doesnot terminate the loop up to the end of the array.

    Code:
    #define MAX 10
    int n, m;
    
    
    //CODE FRACTMENT.
    
    
    for( int i = 0; i < MAX && n != '\n'; i++)
       n= scanf(" %d", array[i];
       m = getchar();
    i dont understand why this doesnt work.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187

    correct version

    sorry for the error in the code

    here is the correct version.
    Code:
    for ( int i = 0; i < MAX && m != '\n'; i++)
       n = scanf(" %d", array[i];
       m = getchar();
    
    //this loop doesnt exit after you enter 3 numbers and press '\n'

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by Nyah Check View Post
    sorry for the error in the code

    here is the correct version.
    Code:
    for ( int i = 0; i < MAX && m != '\n'; i++)
       n = scanf(" %d", array[i];
       m = getchar();
    
    //this loop doesnt exit after you enter 3 numbers and press '\n'
    [CODE]

    The code indented right; does this help you understand?
    Code:
    for ( int i = 0; i < MAX && m != '\n'; i++)
       n = scanf(" %d", array[i];
    m = getchar();
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    It would be nice if you posted something which you at least tried to compile, then we wouldn't have to guess which of the many syntax errors are actually irrelevant to your question.
    antred likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    ok i looked at the loop and thought this may be where the problem came from. However @stahta01 thanks for the tip i'll implement that and see what happens.
    @Salem i would check for the complete code and paste for us to see what it gives....do that later....

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by Nyah Check View Post
    ok i looked at the loop and thought this may be where the problem came from. However @stahta01 thanks for the tip i'll implement that and see what happens.
    I DID NOT CHANGE YOUR CODE; I just indent what code you posted!!!!

    Indentation DOES NOT change how the code runs in C!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Douala, Cameroon
    Posts
    187
    ok so what other thing can i do to ensure the loop exits after pressing the '\n' key

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Read these links
    If Statements in C - Cprogramming.com
    scanf - C++ Reference

    Then ask a real question about what you wish to do.
    Post code that compiles and shows the problem.

    Note: I have NO idea why you think you need a for loop instead of a while loop.

    Edit: I would use fgets and sscanf; but, I am just guessing at what you are trying to do.
    (Likely use strchr with them to do what I am guessing you are trying to do. http://www.cplusplus.com/reference/c...string/strchr/)

    Tim S.
    Last edited by stahta01; 06-05-2012 at 02:27 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,570
    Quote Originally Posted by Nyah Check View Post
    ok so what other thing can i do to ensure the loop exits after pressing the '\n' key
    The value of m never changes in your loop; how do you think it will ever end because a '\n' is read?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You should probably be using a do-while loop if you want to terminate based on a certain input:
    Code:
    do {
        /* stuff */
    } while (getchar() != '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating Zero
    By DickArmy in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 01:53 PM
  2. terminating a while loop with a character
    By just_learning in forum C Programming
    Replies: 3
    Last Post: 04-13-2007, 05:22 AM
  3. loop not terminating
    By akira181 in forum C++ Programming
    Replies: 6
    Last Post: 05-21-2006, 07:30 PM
  4. for loop not terminating
    By samGwilliam in forum C++ Programming
    Replies: 20
    Last Post: 04-07-2005, 02:04 PM
  5. Terminating loop properly?
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 12-24-2004, 05:34 AM

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