Thread: terminating a loop with the newline key.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Unhappy terminating a loop with the newline 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
    Buea, Cameroon
    Posts
    197

    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
    4,183
    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();
    "...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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    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
    4,183
    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.
    "...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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    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
    4,183
    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.
    "...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

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...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

  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. Loop terminating early
    By AdamAppleby in forum C Programming
    Replies: 3
    Last Post: 10-24-2018, 01:35 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, 06:34 AM