I have to write a program for class and it is suppose to read in an unknown number of employee records from a text file and each employee string has 47 characters, which includes name, employee number and hours worked, rate, overtime, and a few other variables.

The problem I am having is they are all read in as strings. I can output all of them to the screen no problem but he wants us to calculate:

gross pay = hours * rate + overtime * 1.5

which mean i need to convert my string to an int to do the calculations so i can cout my answer.

also we must calculate net pay = gross - deductions;

and a few other calculations, when i convert it it ends up printing the same output 20 times, and i need the math done but seperate answers for each calculation for each like

every line it does calculations for i need it to spit out an answer and it seems just replace the output reached about it.

Any assistance you can give me with my program would be great and I would really appreciate it.

This is what i have so far.

string info, name, hours, rate, depend, otime;
    stringstream sshours, ssrate, ssdepend, ssotime;
    double gross, hour, rat, depen, otim, fedtax, fica, ctax, udues, net, overtime;
    int count = 0;
    while (! infile)
    {
        cout << "Error opening file  " << endl;
        exit(1);
    }
  
  while (getline (infile, info))
  {
        
        string name = info;
        string last, first, city, uni, empnum, depend, hours, rate, otime;
  
  if ( count < 21, count ++)
  {
        last = name.substr(0,14);
        first = name.substr(15,10);
        city = name.substr(25,1);
        uni = name.substr(26,1);
        empnum = name.substr(27,5);
        hours = name.substr(32,4);
                sshours << hours;
                sshours >> hour;
        rate = name.substr(36,5);
                ssrate << rate;
                ssrate >> rat;
        depend = name.substr(41,2);
                ssrate << depend;
                ssrate >> depen;
        otime = name.substr(43,4);
        ssrate << otime;
        ssrate >> otim;
        
        gross = (hour * rat);
       
        cout << fixed << showpoint << setprecision(2);
        cout << gross << endl;

        }   
  }

Could you post an example of the input data?

Schanz ##Hannelore#####SX1234540.009.551000.0
Liffer## # Umfritz ###### CM2345638.508.670301.5
Ajahb ###Fyndmee #####CX3456739.606.830203.9
Zyzz ## #Abba ########CX4567840.310.910200.5

here is an example where # is actually spaces but i did it this way so u can see how it is setup with spaces and aligned in the file given to us

Hmm, looking closer. Try changing if ( count < 21, count ++) to if ( count++ < 21 ) . Also, and I think this is your problem, call the clear member function for each stringstream since you read it to end-of-file each time:

hours = name.substr(32,4);
sshours << hours;
sshours >> hour;
sshours.clear();
rate = name.substr(36,5);
ssrate << rate;
ssrate >> rat;
ssrate.clear();
depend = name.substr(41,2);
ssrate << depend;
ssrate >> depen;
ssrate.clear();
otime = name.substr(43,4);
ssrate << otime;
ssrate >> otim;
ssrate.clear();

thanks for the help, ill give the beginning part a try, changing the if statement but not exactly sure what you mean by clear member function.

thanks

>not exactly sure what you mean by clear member function.
What's so hard about copying the code I posted into your program? Do I need to come over there and point out the places you should click or something?

hey im sorry i didnt see what you were getting at, that wasn't a very nice comment i appoigize if i seem juvenile in my questions but im trying to learn im not like some scummbags that take programming and post on here asking someone to basically do there work for them.

I have almost of all my problems solved on this program as far as the converting from a string to a double, but the last part of the program here is giving me some trouble, it says to take the city tax and if under city tax ther is a S then output tax = 0, but if under city it says C then output gross times 4%,

likewise for Union if it says X for nonmemeber then output 0 for dues, but if it says M for member then output 4.73% times gross,

all of these pertain to the file as listed in the about example of the file

I APPRECIATE ANY AND ALL HELP!!!

Here is what i have so far:

    string info, name, hours, rate, depend, otime;
    stringstream sshours, ssrate, ssdepend, ssotime;
    double gross, hour, rat, depen, otim, fedtax, fica, ctax, udues, net, overtime,netpay,deductions, citytax, uniondues;
    int count = 0;
    while (! infile)
    {
        cout << "Error opening file  " << endl;
        exit(1);
  }

  while (getline (infile, info))
  {

        string name = info;
        string last, first, city, uni, empnum, depend, hours, rate, otime;

        last = name.substr(0,14);
        first = name.substr(15,10);
        city = name.substr(25,1);
        cin >> city;
        uni = name.substr(26,1);
        empnum = name.substr(27,5);
        hours = name.substr(32,4);
        rate = name.substr(36,5);
        depend = name.substr(41,2);
        otime = name.substr(43,4);

  if ( count++ < 21 )
  {
        hours = name.substr(32,4);
        sshours << hours;
        sshours >> hour;
        sshours.clear();
        rate = name.substr(36,5);
        ssrate << rate;
        ssrate >> rat;
        ssrate.clear();
        depend = name.substr(41,2);
        ssrate << depend;
        ssrate >> depen;
        ssrate.clear();
        otime = name.substr(43,4);
        ssrate << otime;
        ssrate >> otim;
        ssrate.clear();

        gross = ((hour * rat));
        cout << fixed << showpoint << setprecision(2);
        cout << gross << endl;  

        fedtax = ((gross - (depen*17.0))*.1834);
        fica = (gross * .0765);

         if (city == "S")
         {
              ctax = 0.00;
         }
         else if (city == "C")
         {
              ctax = (gross * 0.04);
              }
              ssrate << ctax;
              ssrate >> citytax;

        if (uni == "X")
        { 
              udues = 0.00;
        }   
        else if (city == "M")
        {
             udues = (gross * .0473);
             }
             ssrate << udues;
             ssrate >> uniondues;

        deductions = (fedtax + fica + citytax + uniondues);     

        netpay = (gross - deductions);


        cout << deductions << endl;
Member Avatar for iamthwee

uni = name.substr(26,1);
empnum = name.substr(27,5);
hours = name.substr(32,4);
rate = name.substr(36,5);
depend = name.substr(41,2);
otime = name.substr(43,4);

These appear to be nothing more than magic numbers. Can't you generalise it? At the moment it looks like a disaster waiting to happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.