如何在cpp中读取.dat文件?

时间:2018-11-29 12:02:56

标签: c++ pointers

我正试图让该程序从.dat文件中读取数据,但我所能做的就是在不实际显示文件日期的情况下运行。我在这里想念什么?

"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <iterator>



using namespace std;

void calculation(double &, double &, double &, double & , double &, 
double &, double &,
              double &, double &, double &);



int main()
{


ifstream input_file;
string social_security, last_name, first_name;
double payrate, income_taxrate;
double rhours, ohours, thours;
double netpay, grosspay, overtimepayrate, regularpayrate;
int record_number = 1;
double taxes;

input_file.open ("employee.dat",ios::in);


while(!input_file.eof()&& record_number !=12)
{
input_file >> social_security
            >> last_name >> first_name
            >> thours >> payrate >> income_taxrate;

        regularpayrate = 0;
        overtimepayrate = 0;
        grosspay = 0;
        ohours = 0;

calculation(overtimepayrate, payrate, regularpayrate, grosspay,
            rhours, ohours, income_taxrate, netpay, thours, taxes);
cout << "Record #" << record_number
                         << "\n" << "Employee Social security #: " << 
social_security
                         << "\n" << "Last Name: " << last_name
                         << "\tFirst Name: " << first_name
                         << "\n" << "Hours Worked: "  << thours
                         << "\nPay Rate: $" << payrate;

          //Setting the overtime hours versus regular hours
        if(ohours > 0)
    {
        cout << "\n" << "Regular Pay "
             << 40.0 << " hours @ $"
             << payrate << ": $"
             << regularpayrate
             << "\n" << "Overtime Pay "
             << ohours << " hours @ $"
             << (payrate * 1.5) << " : $"
             << overtimepayrate;
    }
    else
    {
        cout << "\n" << "Regular Pay " << rhours << " hours @ "
             << payrate
             << " $"
             << regularpayrate;
    }
    cout << "\n" << "Gross Pay: $" << grosspay
         << "\n" << "Taxes @ " << (income_taxrate * 100) << "%: $" << 
taxes
         << "\n" << "Net Pay: $" << netpay
         << "\n\n\n";
    record_number++;

}


}


void calculation(double & overtimepayrate, double & payrate, double & 
regularpayrate
             , double & grosspay , double & rhours, double & ohours, 
double & income_taxrate,
              double & netpay, double & thours, double & taxes)
{



            if (thours> 40)
                {
                    rhours = 40;
                    ohours = thours - 40;
                }

            if (thours < 40)
                {
                    rhours = thours;
                }
//Calculations

                    overtimepayrate = ohours* (payrate*1.5);

                    regularpayrate = rhours * payrate;

                    grosspay = regularpayrate + overtimepayrate;

                    taxes = grosspay * income_taxrate;

                    netpay = grosspay - taxes;

}
void intro(){

    cout << "\n\tHello C++ user, I am a program that uses"
         << "\n\ta pointer function in order to access a"
         << "\n\tparticular data file.\n";


}
"

这是我希望在运行时显示的数据:

  • 123-45-6789柯克·詹姆斯44.7 88.99 0.0175
  • 124-89-0007邦德·简45.6 65.75 0.04
  • 405-77-8911粉扑开花40 33.88 0.03
  • 405-10-9871蓬松毛cup 41.2 45.66 0.047
  • 223-03-8761泡芙泡泡37.8 33.44 0.015
  • 980-11-2201 Joneski Kasey 23.1 10.77 0.033
  • 115-88-7619 Crooke I.M.A. 25.4 88.75 0.02
  • 777-00-1232 Smith Alias 43.5 22.3 0.034
  • 345-89-0022德曼·斯坦56.7 29.45 0.065
  • 210-37-1192 Jones Jeane 48.9 20.33 0.025
  • 103-22-4321 Smith Alias 33.5 19.67 0.063

0 个答案:

没有答案