Thread: How to properly dispose of a newline character

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    Question How to properly dispose of a newline character

    So I have this program:

    Code:
    #include <stdio.h>
    #include <stdint.h>
    
    #define INTERVAL 24
    
    int main()
    {
      int i, n;
    
        printf("This program prints a table of squares. \n");
        printf("Enter number of entries in table: ");
        scanf("%d", &n); 
    
    
        for(i = 1; i <= n; i++)
        {
            printf("%5d% 5d\n", i, i * i);
    
    
            if(i % INTERVAL == 0)
            {
                printf("\nPress enter to continue. ");
    
                // This loops gets skipped because of the new line character that gets left behind once the user inputs something to the variable n
                while(getchar() != '\n')
                {
                    printf("\nYou must press the enter key. ");
                    getchar();
                }
            }
    
            printf("\n");
        }
    
    
        return 0;
    }
    and the only thing wrong with it is that the while loop doesn't run because of the newline that gets left behind once the user enters characters to the variable n. I know a couple of workarounds to get around this but those methods aren't very stable and more short term than long term. So how would I properly dispose of this pesky newline character?
    Last edited by deathslice; 05-10-2015 at 07:50 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What have you tried?

    Jim

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    writing getchar() after the scanf() is one work around but like I said it's not an ideal solution because what if the user enters more than one character? This solution wouldn't work and it's more short term than long term.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    The other popular way of handling this is to use fgets to read the whole input line into a buffer. Provided the buffer is large enough, you'll get anything before and after the number, as well as the newline, so no surprises are left waiting on stdin.
    Then you can use sscanf (string scanf) to parse the number from the buffer.

    Or, you can use scanf as you do now, and use call fgets() afterwards to scoop up any rubbish on stdin. fgets() stops when it encounters a newline, which we'll definitely have from the scanf.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have a look at how this faq suggests you get a line of input from the user
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clarification with newline character
    By Satya in forum C Programming
    Replies: 7
    Last Post: 03-10-2012, 03:49 AM
  2. Replies: 2
    Last Post: 11-12-2010, 12:54 PM
  3. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 07:50 PM
  4. Newline character
    By sean in forum Networking/Device Communication
    Replies: 6
    Last Post: 11-24-2004, 03:33 PM
  5. comparing int to newline character
    By RedZippo in forum C++ Programming
    Replies: 5
    Last Post: 05-13-2004, 06:37 PM

Tags for this Thread