Why wont this code work right

My program totals the rate someone will be paid, based on hours and pay rate.
Then I had to add a way for it to read the information from a file and enter the result in another file.
I almost have it but its not working just right, please help.

It always enters the numbers 40 for hours and 10 for rate in the 2nd file.
I need to to total the variables in the .dat file

Here is my code:

#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::setprecision;

int main ()
{
//Declare and open files
ifstream inData;
ofstream outData;
inData.open ("name.dat");
outData.open ("name.txt");

//Declare variables
string employeeNumber;
string firstName;
string lastName;
string hours;
string rate;




if (!outData.good())
cout << "File could not be opened!" << endl;
else
{

while (inData >> firstName >> lastName >> hours >> rate)
{

////////////////////////////////////////////////////////////////////////////////////
double hours = 40.0;
double rate = 10.0;
double gross = 0.0;
double overtime = 1.5;

if (hours<0 || rate<0)

cout << "ERROR pick a different number" << endl;

else

if(hours > 40){ //calculate gross pay
overtime = (hours-40) * rate * 1.5;
gross = 40 * rate + overtime;

}

else
{
gross = rate * hours;
}
//////////////////////////////////////////////////////////////////////////////////////

outData << firstName << " " << lastName << " " << hours << " " << rate << " " << overtime << " " << gross << " Gross Pay " << endl;

}


}


//// Access middle inital and append a period

//initial = middleName.substr(0,1) + '.';

//// Output information in required formats

//outData <<firstName << ' ' << middleName << ' ' lastName
// << ' ' <<socialNum << endl;
//outData <<lastName << ' ' << firstName << ' ' middleName
// << ' ' <<socialNum << endl;
//outData <<lastName << ' ' << firstName << ' ' initial
// << ' ' <<socialNum << endl;
//outData <<firstName << ' ' << inital << ' ' lastName



// outFile.close();
return 0;
}
Firstly, use [code]code goes here[/code] tags.

Now for the code:
Firstly No need to use both:
using namespace std & using std::something

Secondly:
1
2
3
4
else
      if(hours > 40){	 //calculate gross pay
      overtime = (hours-40) * rate * 1.5;
      gross = 40 * rate + overtime;

to:

1
2
3
4
5
6
else if(hours > 40)
{
      //calculate gross pay
      overtime = (hours-40) * rate * 1.5;
      gross = 40 * rate + overtime;
}

I will try this program out and tell you about the error as soon as I can.
Nisheeth, the only problem with that is that he didn't close the code block. He can put as much white space between else and if as he wants. It's just a readability issue, but if it's easier for him to read, then...
Last edited on
Topic archived. No new replies allowed.