|
-
Jan 6th, 2002, 05:15 AM
#1
Thread Starter
Hyperactive Member
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" );
}
-
Jan 6th, 2002, 07:03 AM
#2
Monday Morning Lunatic
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
-
Jan 6th, 2002, 08:42 AM
#3
Thread Starter
Hyperactive Member
ok.....
1) line 24: warning: passing arg 1 of `output' from incompatible pointer type
2) line 66: parse error at end of input
thnx
-
Jan 6th, 2002, 08:50 AM
#4
Monday Morning Lunatic
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
-
Jan 6th, 2002, 09:09 AM
#5
Thread Starter
Hyperactive Member
thnx
but i also need to leave out the two keywords 'const' to get it to compile correctly. WHy's that?
thnx
-
Jan 6th, 2002, 09:41 AM
#6
Monday Morning Lunatic
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
-
Jan 6th, 2002, 10:07 AM
#7
Thread Starter
Hyperactive Member
my IDE is DevC++, and i'm using that built-in compiler.
-
Jan 6th, 2002, 10:15 AM
#8
Monday Morning Lunatic
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
-
Jan 6th, 2002, 10:42 AM
#9
Thread Starter
Hyperactive Member
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...
-
Jan 6th, 2002, 11:17 AM
#10
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|