My Program compiles but when I go to run it, it close.

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

The txt file is as follows id hw hr ms.

Any help would be appreciated.
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.

Hope this helps.
Last edited on
Thanks, it works now, but is there a way to use letters.
Sure, just >> a letter.

That is was i was using in the txt file. ie 5001 40 7.50 M.
I replaced the M with a # and it ran just fine, but would not run when I used the letter.
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:

scanf( "%f %c %d", &my_float, &my_char, &my_int );

The problem is that the text to data conversion part of the process is obvious in C, but in C++ it is easy to forget what is actually happening.


If you could supply a single line of the kind of input you are expecting, I can help you read and lex it.
Last edited on
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.
More than likely we are in the same class. We seem to be working on the same ongoing payroll program. Are you using a book by a Professor Ebrahimi?

Topic archived. No new replies allowed.