Thread: Scanning input data

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

    Scanning input data

    hi

    i need help with listing sales only for 2009 in various data inputs.

    how do i do that?

    so i need to search the data and print out only the data in 2009.!!

    help needed!!!!!!!!

    thnx

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The answer depends on how the sales data is organized. Usually, if you write your program to mimic how you would do it, by hand, you have a good starting place.

    Generally, one column of the data would be the year's date, and making a struct for the data, and then making an array of those structs, provides a big advantage for programs like this.

    Give it a try, and post up what you have tried, if you get stuck.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    hi

    ok.....this is what i have so far:

    //Stage 2: printing sales that happened in year 2009

    printf("\n-----------------------------------------------\nStage2\n-----------------------------------------------\n");

    printf("Listing of all the sales recorded for the year 2009:\n");
    printf("+========================================= +\n| Postcodes Month Year Bedrooms Price |\n+-----------------------------------------+\n");

    int i;
    for(i=0; Year[i]<2009; i++) {
    printf("| %4ld %02d %4ld %d %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);


    ok.....so what i am supposed to do is scan the input data(which can be like 10 000 lines long) and print out for the year 2009 how many sales took place etc...
    how do i do that by using arrays?

    thank you

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Please post your code in code tags next time.

    It is much better to ask about things you are having trouble with then asking how to do such broad and general things. You probably want to start reading about structures (they're fairly simple) and once you can read data for one sale into a structure, try making an array of them.

    Once you know how to use an array of structures, and you can read data into one structure, you should be able to complete your task easily.

    (If you just want to use several arrays for each elements it's also do-able, but much messier.)

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    hi

    ok...thnx for that.....

    we have not been taught structures so i dont think we can use them....

    also this is what i have

    Code:
    int read_num(int [2009], int *num);{
             int num;
             getchar();
    
         while(ch!='\n')
               scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch);
    
      while(ch!='\n')
      while(
            scanf("%d", &num) ==1); {
            if (num<=2009) {
    }
            printf("|  %4ld    %02d   %4ld     %d        %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
    }
    }
    and in the sample10.txt: the last line is printed out instead of actually printing the sales only for 2009.
    any ideas?

    i dont think my program scans the whole data and reads the 2009 sales only..

    thank you so much for ur help.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    If that code compiles then your compiler is broken.

    If you have not yet learnt how to write functions, it's probably best to stay away from them, at least until you have finished this particular program. You seem to be biting off more than you can chew. Of course, I'm assuming this homework and it is due sometime, if it's just for learning purposes then knock yourself out, you might find it difficult though.

    Do you need to hold the data in memory or just print it out?

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    actually i just need the print out of it.

    so i am suppose to scan the file and print out the data for only the sales that happened in year 2009.

    i hope that makes it clearer/

    thnks

  8. #8
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Ok, I think I see what you're trying to do now.

    Depending on the format of the data for a particular sale, you can just read in all the data for one sale, then test whether the year is equal to 2009 and if so, print it. You've sort of got the right idea but your execution is a little off. :P

    for some pseudocode
    Code:
    while there is still input
            read the data on the line into the respective variables
            if year == 2009
                    print the variables
    repeat
    This method only requires some basic knowledge of loops and printf/scanf.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    ok...so i tried to do that aswell.....

    however....it still doesnt work

    this is what i have:
    Code:
    getchar();
             while(ch!='\n')
                   scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch);
    
      while(ch!='\n')
      while(
            scanf("%ld     %d  %ld    %d    %ld\n", &Postcodes, &Month, &Year, &Bedrooms, &Price)!=EOF);{
            if (Year==2009){
    }
    }
            printf("|  %4ld    %02d   %4ld     %d        %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
    so can i use arrays for postcodes only?
    and then tell it to scan etc....

    thnx

  10. #10
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    getchar();
             while(ch!='\n')
                   scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch);
    I'm not sure what the point of this code here is? It's just reading five characters the last of which will be in ch.

    Code:
      while(ch!='\n')
      while(
            scanf("%ld     %d  %ld    %d    %ld\n", &Postcodes, &Month, &Year, &Bedrooms, &Price)!=EOF);{
            if (Year==2009){
    }
    }
            printf("|  %4ld    %02d   %4ld     %d        %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
    You seem you have repeated 'while(ch!='\n')' for some reason. When scanf reads the data in, it will discard whitespace (depending on the format specifiers, but yours are fine). Therefore you only need to test for when there is no more input. If the lines of data are on different lines scanf will take care of that for you, as long as there that data is always there.

    Make sure you have your format specifier in scanf correct, this is the format that the data will be in.
    Code:
    "%ld     %d  %ld    %d    %ld\n"
    means that there will be a long integer, then 5 spaces then an integer then 2 spaces so on. If you describe the way that the data is formatted then we can tell if it's correct or not.

    Your while loop isn't actually executing the if statement, even if it returns true. You have place a semi colon at the end of the while loop.

    Also, your print statement isn't inside your if statement, so if the loop executes it will print regardless of whether the year is 2009 or not.
    Last edited by DeadPlanet; 04-18-2010 at 02:51 AM.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    hi

    ok...thank u very much for that...

    also i want to store the input data(by stdin) in to 2 d arrays

    can u plz tell me how to do that?
    this is what i have so far:
    Code:
    #define ROWS 5
    #DEFINE COLUMNS 10000
    
    //#define MAXVALS 10000
      //int read_int_array(int[], int);
      //void sort_int_array(int[], int);
    
      int main(int argc, char *argv[]) {
           //declaration & initialisation
          long Postcodes=0;
           int Month=0;
           long Year=0;
           int Bedrooms=0;
           long Price=0;
           char ch=0;;
       int array2d[ROWS][COLUMS]
    
    {                   data[n][0] = Postcodes;
                       data[n][1] = Month;
                       data[n][2] = Year;
                       data[n][3] = Bedrooms;
                       data[n][4] = Price;
                    n++;
    }
            }
    thnx in advanced

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Papermate, do you need to re-organize the 2009 data lines, once you find them OR do you just need to print every line (row) of data, for 2009, exactly as it is?

    Because if you just have to find and print out those 2009 lines of data, then there's a very easy way to do this -- way easier than what you're trying to do now.

    And I'm all over easy, don'cha knooow!

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    ok...we need to examine the input data from stdin

    so no i dont have to re-organise the 2009 data lines just print out every row...

    so can u please tell me the easy way?

    thanks

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by papermate View Post
    ok...we need to examine the input data from stdin

    so no i dont have to re-organise the 2009 data lines just print out every row...

    so can u please tell me the easy way?

    thanks
    Nobody is going to enter 10,000 lines of data, by hand. How is the data arriving - and no fibbing!

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    well.. yeh no one will do that

    however....when they runs my program they will do this:
    ./filename <sample10000.txt
    so its the same as typing it in

    ok. so can u plz tell me the code now?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  4. Need a lesson on input and output data
    By dispatch4599 in forum C++ Programming
    Replies: 16
    Last Post: 02-06-2008, 09:04 PM
  5. 2D SafeArrays- input data
    By therealwill in forum Windows Programming
    Replies: 4
    Last Post: 05-23-2006, 03:55 PM