943,774 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4008
  • C++ RSS
Oct 6th, 2008
0

Any help with C++ payroll! -noob

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int numberofemployees;
  8. int employeeid, hoursworked;
  9. float hourlyrate, grosspay, taxamount, netpay;
  10. const float TAXRATE=0.10;
  11. ifstream fin("C:\Dev-CPP\employee.txt");
  12.  
  13. while (!fin.eof() )}
  14. fin >> employeeid >> hoursworked >> hourlyrate;
  15. cout<<"EMPLOYEE ID IS: "<<employeeid<<endl;
  16. cout<<"THE HOURS WORKED ARE: "<<hoursworked<<endl;
  17. cout<<"THE HOURLY RATE IS: "<<hourlyrate<<endl;
  18. grosspay=hoursworked*hourlyrate;
  19. taxamount=grosspay*TAXRATE;
  20. netpay=grosspay-taxamount;
  21. cout<<"THE GROSSPAY IS"<<grosspay<<endl;
  22. cout<<"THE TAX AMOUNT IS"<<taxamount<<endl;
  23. cout<<"THE NETPAY IS"<<netpay<<endl;
  24. }
  25.  
  26. fin.close ();
  27. // system("pause");
  28. return 0;
  29. }//MAIN
- Using Bloodshed Dev C++ -
The name of the project sourcefile is payollin.cpp and document employee.txt
are in same folder (C:\Dev-Cpp).
Last edited by Narue; Oct 7th, 2008 at 3:41 pm. Reason: fixed code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
hapiscrap is offline Offline
26 posts
since Aug 2007
Oct 6th, 2008
0

Re: Any help with C++ payroll! -noob

You need to explain what problem you have with this code. Also it should be

[code=cplusplus]
Last edited by stilllearning; Oct 6th, 2008 at 6:25 pm.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 7th, 2008
0

Re: Any help with C++ payroll! -noob

"C:\Dev-CPP\employee.txt" may not correct , do not you need escape \ using \\ ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Oct 7th, 2008
0

Re: Any help with C++ payroll! -noob

It would be good if you could tell us what problems you're facing. So i'm presuming that employee.txt is an infile? Please upload it.
Featured Poster
Reputation Points: 129
Solved Threads: 26
Nearly a Posting Maven
zandiago is offline Offline
2,463 posts
since Jun 2007
Oct 7th, 2008
0

Re: Any help with C++ payroll! -noob

[code=cplusplus]

#include <iostream>
#include <fstream>

using namespace std;

int main() {
int numberofemployees;
int employeeid, hoursworked;
float hourlyrate, grosspay, taxamount, netpay;
const float TAXRATE=0.10;
ifstream fin("C:\Dev-Cpp\employee.txt");

while (!fin.eof()){
fin >> employeeid >> hoursworked >> hourlyrate;
cout<<"EMPLOYEE ID IS: "<<employeeid<<endl;
cout<<"THE HOURS WORKED ARE: "<<hoursworked<<endl;
cout<<"THE HOURLY RATE IS: "<<hourlyrate<<endl;
grosspay=hoursworked*hourlyrate;
taxamount=grosspay*TAXRATE;
netpay=grosspay-taxamount;
cout<<"THE GROSSPAY IS"<<grosspay<<endl;
cout<<"THE TAX AMOUNT IS"<<taxamount<<endl;
cout<<"THE NETPAY IS"<<netpay<<endl;
}//WHILE
return 0;
}//MAIN



ihe infile is employee.txt, here is file......



9380 35 18.00
0450 42 16.00
7834 45 10.00
4319 27 22.00
8899 40 18.00

when I compile and run it does not stop!
Reputation Points: 10
Solved Threads: 0
Light Poster
hapiscrap is offline Offline
26 posts
since Aug 2007
Oct 7th, 2008
0

Re: Any help with C++ payroll! -noob

You need a [/code] at the end of your statements.

refer to what ithelp said above. You need to escape your \ inside the quotes, so your file name string probably needs to be "C:\\Dev-Cpp\\employee.txt"

Also it is better to explicitly state the mode in which you want to open the file, and then check if the file pointer is valid or not before proceeding to try and read the contents of your file.
ifstream ifs ( "test.txt" , ifstream::in );

if(ifs.good()) { // we have a valid pointer, proceed

................
}
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Oct 7th, 2008
0

Re: Any help with C++ payroll! -noob

Hey, Are you sure that the file is being opened?
Because Your Code Doesnt Test whether it has opened it

C++ Syntax (Toggle Plain Text)
  1. if (myfile.is_open()) { /* ok, proceed with output */ }

You should have an if statement like the above to know that you have opened the file successfully.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: listing files in a drive
Next Thread in C++ Forum Timeline: Help me find my memory leak!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC