User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,489 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,715 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 463 | Replies: 3
Reply
Join Date: Sep 2007
Posts: 2
Reputation: Solidsteel86 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Solidsteel86's Avatar
Solidsteel86 Solidsteel86 is offline Offline
Newbie Poster

Homework help

  #1  
Sep 19th, 2007
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.

  1. #include <stdio.h>
  2. #include <math.h>
  3. main ()
  4. {
  5. int id;
  6. double com, wit, sales;
  7. FILE *inp;
  8. inp=fopen ("prog2.dat", "r");
  9. while (fscanf (inp, "%d %d", &id, &sales) !=EOF)
  10. {
  11. if (sales <=30000)
  12. {com= (.2*sales)}
  13. else
  14. {com =6000+(.2*sales)}
  15. if (com <=3000)
  16. {wit=.05*com}
  17. else if (3000<com && com<=6000)
  18. {wit=150+(.07*com)}
  19. else if (6000<com && com<=10000)
  20. {wit=360+(.09*com)}
  21. if (10000<com)
  22. {wit=720+(.12*com)}
  23. printf("ID=%d Commission=$%8.2f Withholding_Tax=$%5.2f\2", id,com,wit);
  24. }
  25. }

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Homework help

  #2  
Sep 19th, 2007
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).
Reply With Quote  
Join Date: Sep 2004
Posts: 6,515
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 489
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Homework help

  #3  
Sep 19th, 2007
>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);}
I'm here to prove you wrong.
Reply With Quote  
Join Date: Sep 2007
Posts: 2
Reputation: Solidsteel86 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Solidsteel86's Avatar
Solidsteel86 Solidsteel86 is offline Offline
Newbie Poster

Re: Homework help

  #4  
Sep 19th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 3:14 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC