Two compile errors i need help with.

This is a discussion on Two compile errors i need help with. within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a program (exercise from a book) that reads in information about salespersons and products ...

  1. #1
    Unregistered
    Guest

    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
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Removed const and added a }.

    #include <stdio.h>
    #define PERSON 4
    #define PRODUCT 5

    /* Function prototype */
    void output( 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( 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" );

    }

  3. #3
    Unregistered
    Guest
    Hi,

    thnx, i tried your code and it works fine. But when i added a '}' and remove the 'const', theres still the error 'parse error at end of output', i compared with your code and i couldn't find the difference in the 'output' function. pls help once more. Also, why does the 'const' keyword have to be taken away?

    thnx in advance!

    My edited code:

    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( 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( 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" );
    
    }

  4. #4
    Unregistered
    Guest
    Hey, sorry my mistake, my code works fine now, but can u pls still tell me why to leave out 'const' ?

    thnx

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: Two compile errors i need help with.

    Originally posted by Unregistered
    Also if possible, is there a way to make the code even better?
    Lines 16 and 17, from main():
    Code:
    for ( i = 0; i <= PERSON - 1; i++ ) {
      for ( j = 0; j <= PRODUCT - 1; j++ ) {
    i < PERSON achieves the same result as i <= PERSON - 1. In both cases, the condition is true as long as i is less than four. The change achieves a small speed increase, as you're no longer doing subtraction in each iteration of the loop. The same change can be made in the next line, making the test condition j < PRODUCT.

    From output():
    Code:
    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 ] );
    This may be rewritten as:
    Code:
    printf( "\nTotals" );
    
    /* reusing variables i, j and total */
    for ( i=0; i < PERSON; i++ )
    {
      total = 0;
    
      for ( j=0; j < PRODUCT; j++ )
        total += array[i][j];
    
      printf( "%4d\n", total );
    }
    I confess that one reason for this change is cosmetic (personal preference) . However, a legitimate advantage this change has is that the values of PERSON and PRODUCT may be changed in the future without having to change any code here. For example, if the store hired additional salespeople or introduced a new product, it would be much easier to change the preprocessor definitions of PERSON and PRODUCT than it would to rewrite the output() function each time.

    Hope that helps!
    Jason Deckard

  6. #6
    Unregistered
    Guest
    thnx very much jason. Hope one day can be like u.

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I removed const because GCC didn't accept it. But now, when compiling it with LCC, it does. According to the standard const should be supported. But perhaps I have an older version of GCC.

  8. #8
    Unregistered
    Guest
    Wait can u help me once more? It's missing only that bit for my exercise. Below is my current code. I want to add 'Product 1' on the first line of the totals and 'Product 2' on the 2nd line of the totals. It proves difficult to me sinze the outer loop is smaller than the inner loop. Pls help.

    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( 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 (for sales-person %d) of product %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( int array[][ PRODUCT ], int person, int product )
    {
       int i, j, k, total = 0;
    
       printf( "\n                            ***Report***\n\n" );   /* Print title */
    
       printf( "           %-10s%9s%11s%11s%13s", "Person 1", "Person 2", "Person 3", "Person 4",
               "Totals" );            /* Column headers */
    
       for ( j = 0; j <= product - 1; j++ ) {
          printf( "\n" );
          printf( "     " );
          for ( i = 0; i <= person - 1; i++ ) {
             if ( array[ i ][ j ] == -1 )
                printf( "%10s", "/" );
             else {
                printf( "%10d", array[ i ][ j ] );
                total += array[ i][ j];
             }
             if ( i == 3 )
                printf( "%16d", total );
          }
    
          total = 0;
    
       }
    
       printf( "\nTotals" );
       printf( "%10d", array[ 0 ][ 0 ] + array[ 0 ][ 1 ] + array[ 0 ][ 2 ] + array[ 0 ][ 3 ] + array[ 0 ][ 4 ] );
       printf( "%10d", array[ 1 ][ 0 ] + array[ 1 ][ 1 ] + array[ 1 ][ 2 ] + array[ 1 ][ 3 ] + array[ 1 ][ 4 ] );
       printf( "%10d", array[ 2 ][ 0 ] + array[ 2 ][ 1 ] + array[ 2 ][ 2 ] + array[ 2 ][ 3 ] + array[ 2 ][ 4 ] );
       printf( "%10d", array[ 3 ][ 0 ] + array[ 3 ][ 1 ] + array[ 3 ][ 2 ] + array[ 3 ][ 3 ] + array[ 3 ][ 4 ] );
    
       printf( "\n\n" );
       printf( "            " );
       for ( k = 0; k <= 50; k++ )
          printf( "*" );
       printf( "\n" );
    
    }

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    > but can u pls still tell me why to leave out 'const'
    The short answer is you shouldn't have to - your original use of const was correct.

    It's fine when compiled with vc++

    But for some un-obvious reason, it generates a warning with gcc.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I do not quite understand.

    >... sinze the outer loop is smaller than the inner loop

    You're inner loop runs from 0 to 3 and the outer loop runs from 0 to 4.

    Since the product-loop is the outer loop, the first line will contain the totals for product 1, the second for product 2 etc.

  11. #11
    Unregistered
    Guest
    Hi,

    My mistake again, it works fine now. But it's not possible when the outer loop is smaller right?

    thnx
    all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange compile errors
    By csonx_p in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2008, 11:41 AM
  2. compile once, compile twice ...error
    By Benzakhar in forum Windows Programming
    Replies: 6
    Last Post: 12-28-2003, 05:00 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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