Im working on a payroll program as well using dev c++ 5.0. I beleive I have it scripted right. It compiles just fine with no errors, but the windo wont stay open. This what I have written.
#include<iostream>
#include<fstream>
using namespace std;
main(){
int id, hw, hr, ms;
float gp, tax, TAXR, np;
ifstream fin("payrollm.txt");
while(fin>>id>>hw>>hr>>ms){
gp=hw*hr;
if ((gp>1000) && ms=='S'||ms=='s') TAXR=0.35;
else if ((gp>1000) && ms=='M'||ms=='m') TAXR=0.30;
else if ((gp>1000) && ms=='H'||ms=='h') TAXR=0.25;
else if ((gp>800) && ms=='S'||ms=='s') TAXR=0.25;
else if ((gp>800) && ms=='M'||ms=='m') TAXR=0.20;
else if ((gp>800) && ms=='H'||ms=='H') TAXR=0.15;
else if ((gp>500) && ms=='S'||ms=='s') TAXR=0.15;
else if ((gp>500) && ms=='M'||ms=='m') TAXR=0.10;
else if ((gp>500) && ms=='H'||ms=='h') TAXR=0.05;
else TAXR=0.0;
tax=gp*TAXR;
np=gp-tax;
cout<<"EMPLOYEE ID IS : "<<id<<endl;
cout<<"EMPLOYEE'S MARITAL STATUS IS: "<<ms<<endl;
cout<<"THE HOURS WORKED ARE : "<<hw<<endl;
cout<<"THE HOURLY RATE IS : "<<hr<<endl;
cout<<"THE GROSSPAY IS : "<<gp<<endl;
cout<<"THE TAXAMOUNT IS : "<<tax<<endl;
cout<<"THE NETPAY IS : "<<np<<endl;
system("pause");
}//WHILE
return 0;
}//main
Your input variables are all ints, so if your text file contains anything else it will fail. For example:
1 2 3
EmployeeID HoursWorked HourlyRate MaritalStatus
12 43.0 7.50 single
3 50.0 24.00 married
This example will fail because the very first line contains things that are not numbers. If that line were not there, it would fail at "43.0", because '.' is not part of an integer. So if you fixed that by making all the ints into doubles, then it would fail at "single". Etc.
Here's another example gone wrong:
1 2
12, 430, 750, 1
3, 500, 2400, 2
This one fails at the first comma.
Hope this makes sense. Your input file must exactly match the input variables, otherwise it will fail:
1 2
12 430 750 1
3 500 2400 2
This last one will work, because it contains nothing but integer numbers and whitespace.
Well, the problem is that (even in the vast majority of C++ textbooks and courses) there is the unfortunate wont to confound reading input and lexing input. While related, they are separate things.
If you keep in mind that every time you see something like:
cin >> my_float >> my_char >> my_int;
in C++ it has the same basic meaning as the following in C:
I figured out what I was doing wrong from your help.
Light bulb went off a little while ago.
I forgot ms should be a char and not a int.
Thank you for your help.