Thread: Help with a bit of tweeking on form feed

  1. #1
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    Smile Help with a bit of tweeking on form feed

    Hey Forum
    can any one help me out by taking a look at this code, any suggestions are welcome(or critical)
    The problem to solve is, a greengrocer has his daily produce enterd onto a magnetic disk at the end of each day.
    The DAT file is already sorted serialy by type code and each rec ends with the (\n)
    This prog is supposed to run a report off via the LPT1 port
    and accumulate the totals of each product, and then the grand totals including profits(or non-profits)
    The problem i have is how to set up the form feed i.e line counts,
    and form feed(syntax)
    Excuse the code as I'm still working on it, I've tried to describe what everthing does.
    all help welcome

    /*fill in a load of comments about the program*/
    #include<stdio.h>
    #include<time.h>

    #define SIZE sizeof(struct stock_rec)
    #define LINES_PER_PAGE 60 //length of page I presume

    struct stock_rec { // the structure
    char ref_num[5];
    int type_code[5];
    int weight;
    int quantity;
    int calc_method[1];
    float buy_price;
    float sale_price;
    };

    void print_report(struct stock_rec *out_str); //the user defined functions
    int error(int i);
    float calc_meth1(float b_val); //these are not yet complete
    float calc_meth2(float s_val); //and are not a problem
    float gross_profit(float p_val);

    void clrscr(void); //probably won’t use this

    time_t time_in_secs; //global variables
    float value;
    FILE *in_data_fp, *out_prn_ptr;

    //Right I’ve used these arrays to help with the formatting of the ouput
    //as we are taught not to use (\t) due to individual machines set up

    char title[][10] ={ "Buying","Sale","Profit","Reference","Type",
    "weight","Qty","Calc","Price","Value","No","Kilos" ,
    "Meth","\0"
    };

    char foot[][30] = {"Totals potatoes","Totals turnips","Totals peas",
    "Totals carrots","Totals sprouts","Totals tinned_Veg",
    "Calc meth 1= weight * price","2= qty * price","\0"
    };

    char symbol[][1] = {'P','$'};

    void main()
    {

    struct stock_rec *prod_ptr; //pointer to struct

    static int tot_b_price=0, tot_s_price=0; //use of static for permanent address
    static int count, lines;
    static int **code_ptr1, **code_ptr2;


    if( ( out_prn_ptr = fopen("LPT1", "w")) == NULL)
    error(1);

    if( ( in_data_fp = fopen("A:PRODUCE.DAT", "r")) == NULL)
    error(2);

    while(!EOF)
    {
    if( (fread(&prod_ptr, SIZE, 1, in_data_fp)) !=1)
    error(3);

    *code_ptr1=prod_ptr->type_code; //I’ve a pointer to pointer so I
    lines=0; //test type code for change
    //any idea’s welcome
    while(!'\n')
    {

    *code_ptr2 = *code_ptr1+1; //assign the type code pointers

    //if the method is 1 or 2 then pass to a function
    if(&prod_ptr->calc_method[0] != 0)
    calc_meth1(value);
    else
    calc_meth2(value);
    //accumulate the totals then use in another function later
    tot_b_price+=prod_ptr->buy_price;
    tot_s_price+=prod_ptr->sale_price;

    lines++; //not sure if I should use this here
    code_ptr1++;
    }

    gross_profit(value);
    print_report(prod_ptr); //output the record to the printer
    code_ptr2++;

    if(code_ptr1!=code_ptr2) //test the type code here
    gross_profit(value);
    }
    printf("\nEnd of daily stock report");
    fclose(out_prn_ptr);
    fclose(in_data_fp);

    }




    int error(int i) //error func
    {
    char *e[ ] = {"\nUnable to access printer\nProgram halted",
    "\nUnable to open file at A: drive\nProgram abandoned",
    "\nUnable to read data\n File corrupt"};


    printf("\a%s", e[i]); //sound bell and display msg
    }




    void print_report(struct stock_rec *out_str)
    {
    int count, lines;
    struct tm todays_date;

    time_in_secs = time(NULL);
    todays_date = *localtime(&time_in_secs);

    printf("\tMr Greens (Wholesaler) - Daily Sales Report");
    printf("%13d/%d/%d", todays_date.tm_mday,todays_date.tm_mon+1,todays_da te.tm_year);
    printf("\t\tPage %4d", count);
    printf("\n%40s %20s %20s", title[0],title[1],title[2]);
    printf("\n%9s %6s %9s %6s %6s %9s %11s %13s %11s",title[3],title[4],
    title[5], title[6], title[7], title[8], title[9], title[10],
    title[11]);
    printf("\n%7s %16s %12s %8c %9c %12c %9c", title[11], title[12],title[13],
    symbol[1], symbol[2], symbol[1], symbol[2]);

    //the above may look a bit messy, but I’ve run through my compiler and
    //everything works ok, but the problem stats here, the variable may need //attention but as I said any idea’s are welcome
    //with the line count and the form feed

    for(count=0; count!=EOF; count++)
    {
    printf("\n\n%8.6s %5.1d %10.4d %7.4d %5.1d %10.5f %10.5f"
    "%17.5f %10.7f", out_str->ref_num, out_str->type_code,
    out_str->weight, out_str->quantity, out_str->calc_method,
    out_str->buy_price, out_str->sale_price);

    if(lines>=LINES_PER_PAGE)
    printf("\f");
    }


    }
    //please ignore the rest
    float calc_meth1(float b_val)
    {


    }
    float calc_meth2(float s_val)
    {

    }
    float gross_profit(float p_val)
    {

    }

    again sorry for the length of the code, but you might say that i need a little more help than i thought


    Thank in adce









    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > time_t time_in_secs; //global variables
    > float value;
    > FILE *in_data_fp, *out_prn_ptr;

    In most cases, globals are frowned upon. They typically make code harder to read and follow. They do have their uses, just not here.

    > char symbol[][1] = {'P','$'};

    A single dimension array is all you need.

    char symbol[2] = { 'P','$' };

    > void main()

    main returns an integer. It doesn't matter if you like it, if your teacher likes, it's simply how it works. main ALWAYS returns an integer, there is no exception to this rule.

    > static int tot_b_price=0, tot_s_price=0; //use of static for permanent address
    > static int count, lines;
    > static int **code_ptr1, **code_ptr2;

    Why are these static? You aren't calling 'main' recursivly (a no-no anyway), so there is no point in these being static. Additionally, static variables are given a default value of 0, so if you decide to use static variables, even though there is no need, they are already initialized to zero.

    > while(!EOF)

    This will always be true. You aren't comparing EOF to anything.

    while( (c=fgetc( myInputStream ) != EOF )

    That'd be a better choice.

    > if( (fread(&prod_ptr, SIZE, 1, in_data_fp)) !=1)

    Um. No. You haven't allocated anything for 'prod_ptr'. Without doing so, you're reading into nowhere.

    prod_ptr = malloc( SIZE );
    if( fread( prod_ptr, SIZE, 1, in_data_fp ) != 1 )

    Additionally, I wouldn't use "SIZE", as it's unclear to the casual reader what exactly "SIZE" is. Second, you're reading into the address of a pointer, which is incorrect.

    > if(&prod_ptr->calc_method[0] != 0)

    I think you're not entirely clear on how pointers work. This is also incorrect. You do not need the address sign.

    I'll stop for now (phone call).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let me continue...

    > int error(int i) //error func
    > {
    > char *e[ ] = {"\nUnable to access printer\nProgram halted",
    > "\nUnable to open file at A: drive\nProgram abandoned",
    > "\nUnable to read data\n File corrupt"};
    >
    >
    > printf("\a%s", e[i]); //sound bell and display msg
    > }

    This is very wrong. The reason it's wrong is that you call it with:

    error(3);

    There is no e[3].

    > for(count=0; count!=EOF; count++)
    > {

    Um. No. Very wrong. What did they (whoever is teaching you C) tell you EOF was? EOF typically is -1. As you can see, you'll be looping here for quite some time.

    You need a bit of help on pointers and the correct use of EOF/feof(). Once that's cleared up, you should do much better.
    Code:
    int var, *ptr, newVar;
    
    var = 10; //give 'var' a value
    
    ptr = &var; //give the pointer 'ptr' the address of 'var'
    
    newVar = *ptr; //dereference 'ptr', producing the value contained
                             // in 'var', and assign it to 'newVar'
    
    int c;
    FILE *fp = fopen("myfile","r");
    ...assuming it opens right...
    do {
       c = fgetc( fp ); //read a character from 'fp'
       if( c == EOF ) //test for end of file, this would work
    
       if( feof( fp ) ) //test for end of file, this would work
    } while ( c != EOF || feof( fp ) );
    
    Another example of how to use either of those, you wouldn't
    need both of those in the 'while' check. For 'feof' or 'EOF' to
    work, you must have read something from the file first. If you
    Don't, well, then your check is invalid.
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    Thumbs up

    Thanks quzar you have cleared up a few things for me.
    I have to admit that i'm trying to rush this through as usual.
    Ive got all day today to concentrate on it.
    So with the aid of your constructive critism and actualy checking the course work i'll crack it

    Cheer's
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Worthless comment


    In most cases, globals are frowned upon. They typically make code harder to read and follow.

    Do they?

    They do have their uses, just not here.

    Globals are faster than Locals. You will most likely not notice the difference, but they are faster...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Mr Green says try it yourself first, for no doubt that if you adhere to all advice on this site, then your code and understanding will suffer.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 16 bit compilar or 32 bit compilar
    By rajkumarmadhani in forum C Programming
    Replies: 16
    Last Post: 12-04-2007, 09:48 AM
  2. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  3. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. Form Feed
    By ajchanx in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2001, 12:42 AM