Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 6th, 2013, 05:19 PM
 deathtical
Registered User

Join Date: Jun 2013
Posts: 14
Time spent in forums: 2 h 7 m 28 sec
Reputation Power: 0
I keep getting a compile error and don't understand why

I am coding in C (not C++). I am writing this program that I thought was pretty close to being done but it won't compile. Error is on the line in red. Any help would be great. Thanks.

Code:
```//Use a single-subscripted array to solve the following problem. A company
//pays its sales force on a commission bases. The sales force receive \$200
//per week plus 9% of their gross sales for that week. For example, a sales
//person who grosses \$3000 in sales in a week receives \$200 plus 9% of \$3000,
//or a total of \$470. Write a C program (using an array of counters) that
//determines how many of the sales force earn salaries in each of the
//following ranges (assume that each salesperson's salary is truncated to an
//integer amount):
//
//    \$200-299
//    \$300-399
//    \$400-499
//    \$500-599
//    \$600-699
//    \$700-799
//    \$800-899
//    \$900-999
//    \$1000 and over
//
//Gross sales are entered at program run time. Program will print the number
//of sales force in each salary range. If there are none, then the program
//will print None.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;}

getsales(count);
printranges(count);
return 0;
}

void getsales(int count[])
{
int salary = 0;
float sales = 0;

printf ( "Enter an employee's weekly sales (control-c to end): " );
while ( scanf("%f", &sales) != EOF )
{
salary = 200 + .09 * sales;
if ( salary >= 1000 )
{
count[10]++;
}
else if ( salary >= 200 )
{
count[ salary / 100 ]++;
}
printf ( "Enter an employee's weekly sales (control-c to end): " );
}
}

void printranges( int count[] )
{
int i;

printf( "Employees in salary range: \n" );
for ( i = 2; i < 10; i++ )
{
printf( "\$%d00 - \$%d99: %d\n", i, i, count[i] );
}
printf( "\$1000 and over: %d\n", count[10] );

system("PAUSE");
return 0;
}```

#2
June 6th, 2013, 06:09 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 6 h 2 m 28 sec
Reputation Power: 4106
Code:
`int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;}`

should be:
Code:
`int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};`
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#3
June 6th, 2013, 06:19 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
And just what is that error? Do you want us to try to read your mind? Or play guessing games?

If you have a question about an error message or warning that you're getting, then you need to tell us what it is.

But you're getting a lot more than one error:
Quote:
 C:>g++ -Wall error.cpp error.cpp: In function `int main()': error.cpp:29: parse error before `;' error.cpp:32: implicit declaration of function `int printranges(...)' error.cpp: In function `void getsales(int *)': error.cpp:44: warning: assignment to `int' from `double' error.cpp: In function `void printranges(int *)': error.cpp:69: `return' with a value, in function returning void C:>

The "parse error before `;'" for line 29, the one you indicated, is because it expected a close brace before encountering that semicolon. Try moving the semicolon to the end of the line, on the other side of the close brace.

Correcting that one revealed an additional error:
error.cpp:31: implicit declaration of function `int getsales(...)'

Those two "implicit declaration" errors are because the compiler needs to know about a function before it can call it. Placing prototypes for those two functions above main() should take care of that.

In line 44 you calculate a floating-point value and then you try to assign it to an integer. That will cause a loss of data, namely the fractional part of that value. Do you really want to do that?

And in line 69 you have a return statement that returns a value, but the function is void. Why are you trying to do that?

BTW, always turn warnings on and up. Never ignore warnings. Warnings are far more important than error messages could ever be. Never ignore warnings!

#4
June 6th, 2013, 06:56 PM
 deathtical
Registered User

Join Date: Jun 2013
Posts: 14
Time spent in forums: 2 h 7 m 28 sec
Reputation Power: 0
Quote:
 Originally Posted by Scorpions4ever Your semicolon is inside the closing brace instead of outside it: Code: `int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;}` should be: Code: `int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};`

Thanks. I stared at it for ages and that just didn't jump out at me

Now I am getting a compile error here...
Code:
```void getsales(int count[])
{
int salary = 0;
float sales = 0;```

The error is "conflicting types for 'getsales'". Thanks

Sorry, didn't see dwise1 comment prior to this post. The main reason I am doing or not doing things is because I have only been doing this for a few days and am trying to learn. What do you mean by placing a prototype? Where exactly? Thanks.

#5
June 6th, 2013, 07:26 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
That conflicting types error is because you're effectively redefining a function differently than it has already been "defined".

The compiler works from the top of the source file to the bottom. When it gets to a particular line that references an identifier (eg, a typedef'd datatype, variable, function, etc), then a line somewhere above that line had to already have told the compiler what that identifier is.

If the compiler does not know what that indentifier is, then it reverts to old legacy C behavior that they should have gotten rid of long ago: the compiler assumes that the datatype of the identifier is int. Or in the case of a function, it assumes that the function takes one int argument and returns an int. Then when it encounters the actual identifier declaration/definition further down the file, the compiler gives you an error for redefining that identifier.

That is what is causing that "conflicting types" error. The way you get rid of it is to place function prototypes for those functions up above main().

A function prototype is the function header followed by a semicolon (to show that it's not a function header). For example, the function prototype for getsales would be:
void getsales(int count[]);
Note the semicolon at the end. That way, the compiler will know all it needs to know for when it encounters the call to that function in main().

BTW, most of what's in those header files that you #include are the function prototypes for the library functions.

The main mentality you need to develop is to keep in mind what the compiler knows and what it doesn't know.

#6
June 6th, 2013, 07:37 PM
 deathtical
Registered User

Join Date: Jun 2013
Posts: 14
Time spent in forums: 2 h 7 m 28 sec
Reputation Power: 0
OK. So I made some changes and I am getting a syntax error on the new "line 61".

Code:
```//Use a single-subscripted array to solve the following problem. A company
//pays its sales force on a commission bases. The sales force receive \$200
//per week plus 9% of their gross sales for that week. For example, a sales
//person who grosses \$3000 in sales in a week receives \$200 plus 9% of \$3000,
//or a total of \$470. Write a C program (using an array of counters) that
//determines how many of the sales force earn salaries in each of the
//following ranges (assume that each salesperson's salary is truncated to an
//integer amount):
//
//    \$200-299
//    \$300-399
//    \$400-499
//    \$500-599
//    \$600-699
//    \$700-799
//    \$800-899
//    \$900-999
//    \$1000 and over
//
//Gross sales are entered at program run time. Program will print the number
//of sales force in each salary range. If there are none, then the program
//will print None.

#include <stdio.h>
#include <stdlib.h>

void getsales( int count[] );
void printranges( int count[] );

int main( void )
{
int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

getsales(count);
printranges(count);
return 0;
}

void getsales(int count[])
{
int salary = 0;
float sales = 0;

printf ( "Enter an employee's weekly sales (control-c to end): " );
while ( scanf("%f", &sales) != EOF )
{
salary = 200 + .09 * sales;
if ( salary >= 1000 )
{
count[10]++;
}
else if ( salary >= 200 )
{
count[ salary / 100 ]++;
}
printf ( "Enter an employee's weekly sales (control-c to end): " );
}
}

{
int i;

printf( "Employees in salary range: \n" );
for ( i = 2; i < 10; i++ )
{
printf( "\$%d00 - \$%d99: %d\n", i, i, count[i] );
}
printf( "\$1000 and over: %d\n", count[10] );

system("PAUSE");
return 0;
}```

The remaining errors I get are...

Code:
```61: error: syntax error before '{' token
64: error: syntax error before string constant
64: error: conflicting types for 'printf'
64: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
64: error: conflicting types for 'printf'
64: note: a parameter list with an ellipsis can't match an empty parameter name list declaration
64: warning: data definition has no type or storage class
69: error: syntax error before string constant
69: warning: data definition has no type or storage class
71: error: syntax error before string constant
71: warning: data definition has no type or storage class```

Any further input would be great. Thanks again.

UPDATE:

I managed to get rid a lot of those errors by fixing one line (60). now what I have is this...

Code:
```//Use a single-subscripted array to solve the following problem. A company
//pays its sales force on a commission bases. The sales force receive \$200
//per week plus 9% of their gross sales for that week. For example, a sales
//person who grosses \$3000 in sales in a week receives \$200 plus 9% of \$3000,
//or a total of \$470. Write a C program (using an array of counters) that
//determines how many of the sales force earn salaries in each of the
//following ranges (assume that each salesperson's salary is truncated to an
//integer amount):
//
//    \$200-299
//    \$300-399
//    \$400-499
//    \$500-599
//    \$600-699
//    \$700-799
//    \$800-899
//    \$900-999
//    \$1000 and over
//
//Gross sales are entered at program run time. Program will print the number
//of sales force in each salary range. If there are none, then the program
//will print None.

#include <stdio.h>
#include <stdlib.h>

void getsales( int count[] );
void printranges( int count[] );

int main( void )
{
int count[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

getsales(count);
printranges(count);
return 0;
}

void getsales( int count[] )
{
int salary = 0;
float sales = 0;

printf ( "Enter an employee's weekly sales (ctrl-c to end): " );
while ( scanf("%f", &sales) != EOF )
{
salary = 200 + .09 * sales;
if ( salary >= 1000 )
{
count[10]++;
}
else if ( salary >= 200 )
{
count[ salary / 100 ]++;
}
printf ( "Enter an employee's weekly sales (ctrl-c to end): " );
}
}

void printranges( int count[] )
{
int i;

printf( "Employees in salary range: \n" );
for ( i = 2; i < 10; i++ )
{
printf( "\$%d00 - \$%d99: %d\n", i, i, count[i] );
}
printf( "\$1000 and over: %d\n", count[10] );

system("PAUSE");
return 0;
}```

and here are the remaining errors and warnings...

Code:
```In function `printranges':
72: warning: `return' with a value, in function returning void
Execution terminated
Compilation successful```

Thanks.

#7
June 6th, 2013, 07:54 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
You're starting to panic. Relax. Then look and think.

You had removed the function header for printranges(). Put it back and don't forget to remove the semicolon from it.

After you correct for that, you're still getting:
Quote:
 C:>gcc -Wall error.c error.c: In function `printranges': error.c:72: warning: `return' with a value, in function returning void C:>

In line 72 you are still returning a value from a void function. Void functions do not return any value!

#8
June 6th, 2013, 07:57 PM
 deathtical
Registered User

Join Date: Jun 2013
Posts: 14
Time spent in forums: 2 h 7 m 28 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol You're starting to panic. Relax. Then look and think. You had removed the function header for printranges(). Put it back and don't forget to remove the semicolon from it. After you correct for that, you're still getting: In line 72 you are still returning a value from a void function. Void functions do not return any value!

Yeah. I had just replaced that line before you replied. Guess you were crunching away at it, thanks. I'm not sure I understand your last statement. Thanks.

#9
June 6th, 2013, 07:59 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
OK, it compiled, but it still has warnings. You still need to correct the warnings.

Never ignore warnings and never try to run a program that gives you warnings. Warnings means that your program still has problems. Often, those problems can cause your program to produce unexpected results and even to crash.

Never ignore warnings. Always correct all warnings. And never ignore warnings. Warnings are much more important than error messages are! Never ignore warnings.

#10
June 6th, 2013, 08:05 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,858
Time spent in forums: 3 Months 1 Day 5 h 50 m 16 sec
Reputation Power: 2199
Quote:
 Originally Posted by deathtical Yeah. I had just replaced that line before you replied. Guess you were crunching away at it, thanks. I'm not sure I understand your last statement. Thanks.

What does the return type of a function mean? It means that the function returns a value of that data type.

What happens when you try to return a datatype that is different from the one you declared the function would return? Problems! Sometimes that will cause very severe problems.

What does int main return? An int value, as in return 0;

What does a void function return? Nothing! So what happens when you try to return a value from an void function? Nobody can predict the problems that could cause, because it all depends on what your compiler does when you violate the rules of the C language like that.

The solution: never return a value from a void function.

#11
June 6th, 2013, 08:05 PM
 deathtical
Registered User

Join Date: Jun 2013
Posts: 14
Time spent in forums: 2 h 7 m 28 sec
Reputation Power: 0
Quote:
 Originally Posted by dwise1_aol OK, it compiled, but it still has warnings. You still need to correct the warnings. Never ignore warnings and never try to run a program that gives you warnings. Warnings means that your program still has problems. Often, those problems can cause your program to produce unexpected results and even to crash. Never ignore warnings. Always correct all warnings. And never ignore warnings. Warnings are much more important than error messages are! Never ignore warnings.

So... don't ignore warnings... got it

So it looks like (with a good deal of help) I got the warnings and errors fixed. It compiles and runs with no errors!

Thanks to all that helped.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > I keep getting a compile error and don't understand why