Reading input and removing characters

This is a discussion on Reading input and removing characters within the C Programming forums, part of the General Programming Boards category; Hi everyone I am writing a program where the input data is to be formatted into a table... The input ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    11

    Reading input and removing characters

    Hi everyone I am writing a program where the input data is to be formatted into a table...
    The input looks like this:

    Code:
    Header Header Header Header    Header
    2081       12  2007    4       795500
    2180       07  2009    6       165000
    2140       12  2008    5       689000
    2153       03  2008    4       491500
    2140       02  2010    5       886500
    2005       11  2007    4       776500
    2151       07  2007    5       305500
    2199       12  2010    5       895000
    2101       04  2010    7       389500
    2176       02  2008    2       959500
    So this input data is to be formatted into a table..
    The code i have for my program is this:
    Code:
    #include <stdio.h>
    int main (int argc, char *argv[]) 
    {
    
       int array[10][5];
       int i=0;
       int j=0;
       
       for (i=0;i<10;i++) {
          for (j=0; j<5;j++) {
                   scanf("%d", &array[i][j]);
             }
           }
       printf("\n|");
        for (i=0;i<10;i++) {
           for (j=0; j<5;j++) {  
              printf(" %4d ", array[i][j]);
                printf(" ");
          }
          printf("|\n|");
       }
    
    return 0;
    }
    This code works perfectly IF the headers weren't there.....Because they are characters my resulting table is a mess!!!!!
    I need a way for scanf to ignore the characters....I looked around on the net and saw that i have to use some thing like
    Code:
        char c = ' '; //give c an initial value
    
        //consume characters from the input until we get to the end of the line
        while(c != '\n') {
            scanf("%c", &c);
        }
    BUT i do not know where to place this in the code or how it removes the characters...
    Any help is appreciated...
    Last edited by comp1911; 04-26-2010 at 12:40 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I would put it at the top, right before your first for loop. You want the code snippet you found to consume all characters, then move on to what it is that you really want to do.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Ahhhh thanks....thats solved my problem.....

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Now that my table prints out properly i noticed that the leading zero is not printed out for the second header.....
    Any reason this is so????
    How can this be fixed

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Second header, leading zero? Can you be more specific?

    Edit: I suppose you mean your second column? The reason for that is that 07 for example is really the same thing as 7, a leading zero have no meaning to the value. You need to look up the man page, or a book for printf to get information about format codes. You might have to solve your %4d in another way if you want leading zeroes with printf, look it up.
    Last edited by Subsonics; 04-26-2010 at 01:03 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    11
    Cheers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. looping to get user input
    By james123 in forum C Programming
    Replies: 4
    Last Post: 10-31-2009, 06:51 PM
  2. Tokenizing user input
    By chinesepirate in forum C++ Programming
    Replies: 19
    Last Post: 11-06-2008, 03:05 AM
  3. How to take a string input and remove whitespace?
    By Jasonx521 in forum C Programming
    Replies: 5
    Last Post: 10-06-2006, 10:24 PM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 05:29 PM
  5. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM

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