Results 1 to 10 of 10

Thread: Two compile errors i need help with.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484

    Two compile errors i need help with.

    Hi,

    I am trying to write a program (exercise from a book) that reads in information about salespersons and products they sold and summarize output in tabular format. I am nearly there, when i compile it there are lots of errors (i'm very new 2 c, about 2 weeks), and later i fixed all of them except for two. Pls spot them for me. Thnx in advance! Also if possible, is there a way to make the code even better? Thnx again.

    Code:
    /* Read in salesperson and product information,
       process them and output in tabular format. */
    
    #include <stdio.h>
    #define PERSON 4
    #define PRODUCT 5
    
    /* Function prototype */
    void output( const int array[][ PRODUCT ], int person, int product );
    
    int main()
    {
       int sales[ PERSON ][ PRODUCT ] = { 0 };
       int i, j;                                  /* Loop counters */
    
       for ( i = 0; i <= PERSON - 1; i++ ) {
          for ( j = 0; j <= PRODUCT - 1; j++ ) {
             printf( "Enter cost of product %d for sales-person %d (Enter -1 if nothing): ",i, j);
             scanf( "%d", &sales[ i ][ j ] );     /* Store input in array */
          }
          printf( "\n" );
       }
    
       output( sales, PERSON, PRODUCT );                            /* Print summary */
    
       return 0;
    }
    
    void output( const int array[][ PRODUCT ], int person, int product )
    {
       int i, j, k, total = 0;
    
       printf( "\n            ***Report***\n" );   /* Print title */
    
       printf( "%s%4s%4s%4s%6s", "Person 1", "Person 2", "Person 3", "Person 4",
               "Totals" ); /* Column headers */
    
       for ( i = 0; i <= person + 1; i++ ) {
          printf( "\n" );
          printf( "Product %d", i );
          if ( i != 4 ) {
             for ( j = 0; j <= product; j++ ) {
                if ( array[ i ][ j ] == -1 )
                   printf( "%4s", "/" );
                else {
                   printf( "%4d", array[ i ][ j ] );
                   total += array[ i][ j];
                if ( j == 4 )
                   printf( "%4d\n", total );
             }
           total = 0;
          }
       }
    
       printf( "\nTotals" );
       printf( "%4d", array[ 0 ][ 0 ] + array[ 0 ][ 1 ] + array[ 0 ][ 2 ] + array[ 0 ][ 3 ] + array[ 0 ][ 4 ] );
       printf( "%4d", array[ 1 ][ 0 ] + array[ 1 ][ 1 ] + array[ 1 ][ 2 ] + array[ 1 ][ 3 ] + array[ 1 ][ 4 ] );
       printf( "%4d", array[ 2 ][ 0 ] + array[ 2 ][ 1 ] + array[ 2 ][ 2 ] + array[ 2 ][ 3 ] + array[ 2 ][ 4 ] );
       printf( "%4d", array[ 3 ][ 0 ] + array[ 3 ][ 1 ] + array[ 3 ][ 2 ] + array[ 3 ][ 3 ] + array[ 3 ][ 4 ] );
    
       for ( k = 0; k <= 50; k++ )
          printf( "*" );
       printf( "\n" );
    
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It would help if you posted the errors you got...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    ok.....

    1) line 24: warning: passing arg 1 of `output' from incompatible pointer type

    2) line 66: parse error at end of input

    thnx

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I compiled it as C which avoided the first warning.

    The second one was where you have your "else" clause - you forgot to terminate the block:
    Code:
             for ( j = 0; j <= product; j++ ) {
                if ( array[ i ][ j ] == -1 )
                   printf( "%4s", "/" );
                else {
                   printf( "%4d", array[ i ][ j ] );
                   total += array[ i][ j];
                } /** i changed it here by adding a brace **/
                if ( j == 4 )
                   printf( "%4d\n", total );
             }
    With that change, it compiles, but gives slightly dodgy output - you might want to check whether you're going off the end of your arrays.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    thnx

    but i also need to leave out the two keywords 'const' to get it to compile correctly. WHy's that?

    thnx

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    const was originally a C++ keyword. However, it was transferred into the later C standards, along with function prototypes (developed for C++).

    What compiler are you using?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    my IDE is DevC++, and i'm using that built-in compiler.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think that's GCC...can't see why it shouldn't work.

    Can you run this test program through and see what it says? If it compiles can you include the output as well, please. Make sure it's in a file with a .c extension:
    Code:
    #include <stdio.h>
    
    typedef struct _mystruct {
        int x;
        int y;
    } mystruct;
    
    void myfunc(const mystruct *s);
    
    int main(void) {
        mystruct ms;
    
        ms.x = 5;
        ms.y = 10;
    
        printf("Size: %d\n\n", sizeof(ms));
    
        myfunc(&ms);
    
        return 0;
    }
    
    void myfunc(const mystruct *s) {
        printf("x = %d\ny = %d\n", s->x, s->y);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Posts
    484
    sorry it was my mistake, it was another error and i got it mixed up. FOrgive me i just bought the C book 3 weeks ago...

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    That's okay
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width