Greetings, first time I've posted here. Forgive me if I touch on any taboos unique to this forum. My teacher isn't very good and my textbook is virtually useless or I wouldn't be seeking help on the internet.

I did read the homework sticky, though.

It's basically an exercise in loops and if/else statements.

We're calculating commisions and witholding on sales from a data file we have to access inside the program.

The data file has a setup basically like this, the first number being an employee ID and the second being their sales.

6829 45967.28
1111 25000.11
...
...
2222 22222.00

And the program is as follows, without some of the comments at the beginning that would be used by my teacher.

#include <stdio.h>
#include <math.h>
main ()
{
int id;
double com, wit, sales;
FILE *inp;
inp=fopen ("prog2.dat", "r");
while (fscanf (inp, "%d %d", &id, &sales) !=EOF)
{
if (sales <=30000)
{com= (.2*sales)}
else
{com =6000+(.2*sales)}
if (com <=3000)
{wit=.05*com}
else if (3000<com && com<=6000)
{wit=150+(.07*com)}
else if (6000<com && com<=10000)
{wit=360+(.09*com)}
if (10000<com)
{wit=720+(.12*com)}
printf("ID=%d Commission=$%8.2f Withholding_Tax=$%5.2f\2", id,com,wit);
}
}

My progam keeps spitting out that there's a parse error on my if/else lines. I don't have any idea what that might be.

If some of the program doesn't make sense that's because I don't fully understand the uses of everything and I simply imitated what I've seen before.

Thanks in advance, folks.

Salem commented: A good first post, complete with code tags +10

Recommended Answers

All 3 Replies

Most of the statements in your if/else code lack a ;

Bunching up the code like that doesn't make it any quicker, but does make it a lot harder (therefore longer) to understand.

Indentation is your friend, make use of it.

if (sales <=30000) {
    com= (.2*sales);
} else {
    com =6000+(.2*sales);
}

> fscanf (inp, "%d %d", &id, &sales)
Unless you have a compiler which will warn you, you need to be especially careful about making sure the control string and the parameters match up (%d doesn't read into a double for example).

>main ()
The main function is defined as:

int main ( void )
{
  return 0;
}

Place your code before the return statement. Anything else is wrong. As it is, you're relying on an obsolescent feature (implicit int) and neglecting to return a value, which is undefined behavior.

>{com= (.2*sales)}
Your formatting is hiding the problem, and the problem is failing to end a statement with a semicolon:

{com= (.2*sales);}

Got it working, thanks for the help y'all. I wrote the program late last night, forgetting semi-colons seems pretty dumb right now.

I appreciate it, if I have any more trouble with it I'll post again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.