| | |
Any help with C++ payroll! -noob
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 26
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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; } fin.close (); // system("pause"); return 0; }//MAIN
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
"C:\Dev-CPP\employee.txt" may not correct , do not you need escape \ using \\ ?
•
•
Join Date: Aug 2007
Posts: 26
Reputation:
Solved Threads: 0
[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!
#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!
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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
................
} Hey, Are you sure that the file is being opened?
Because Your Code Doesnt Test whether it has opened it
You should have an if statement like the above to know that you have opened the file successfully.
Because Your Code Doesnt Test whether it has opened it
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Help - While loop won't work (Java)
Other Threads in the C++ Forum
- Previous Thread: listing files in a drive
- Next Thread: Help me find my memory leak!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






