i read from a file okay
for each line i save the respective data into their respective variables
i output each variable to see if the data has really been saved.(is has)
but when i try using the if condition to check the variable and perform a function
it doesn't work and always go to the last else condition and displays "Not Working", Please Help.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int nEmployeeNumber = 0;
    char strEmployeeName[128];
    float fHourlyRate = 0.00;
    float nNoofhoursPerWeek = 0.00;
    string strEmployeeCategory;

    float fOvertime = 0.00;
    float fSalary = 0.00;
    float fTotalIncome = 0.00;
    float fNetIncome = 0.00;
    float fTax = 0.00;

    ifstream empin;
    empin.open("employees.txt", ios::in);

   if(empin >> nEmployeeNumber >> strEmployeeName >> fHourlyRate >> nNoofhoursPerWeek >> strEmployeeCategory)
   {
      cout << nEmployeeNumber << " " << strEmployeeName << " " << fHourlyRate << " " << nNoofhoursPerWeek << " " << strEmployeeCategory << endl;

      if(nNoofhoursPerWeek > 40.00 && strEmployeeCategory == "J")
      {
          fOvertime = nNoofhoursPerWeek - 40.00;
          fOvertime = fOvertime * (fHourlyRate * 1.5);
          cout << fOvertime << endl;

          fSalary = fHourlyRate * nNoofhoursPerWeek * 4.00;
          cout << fSalary;
          fTotalIncome = fSalary + fOvertime;
          cout << fTotalIncome << endl;

          if(fTotalIncome > 1000)
          {
              fTax = (17/100) * fTotalIncome;
              cout << fTax << endl;
          }
          else if(fTotalIncome <= 1000)
          {
              fTax = (10/100) * fTotalIncome;
              cout << fTax << endl;
          }

          fNetIncome = fTotalIncome - fTax;
           cout << "\n";
      }
      else if(nNoofhoursPerWeek == 40)
      {

          fSalary = fHourlyRate * nNoofhoursPerWeek * 4;
          cout << fSalary << endl;
          fTotalIncome = fSalary + fOvertime;
          cout << fTotalIncome << endl;

          if(fTotalIncome > 1000)
          {
              fTax = (17/100) * fTotalIncome;
              cout << fTax << endl;
          }
          if(fTotalIncome <= 1000)
          {
              fTax = (10/100) * fTotalIncome;
              cout << fTax << endl;
          }

          fNetIncome = fTotalIncome - fTax;
          cout << fNetIncome << endl;
          cout << "\n";
      }

      else
      {
        cout << "Not Working" << endl;
      }

      ofstream empout;
      empout.open("employees_out.txt", ios::out | ios::app);

      if(empout.good())
      {
          empout << nEmployeeNumber << " " << strEmployeeName << " " << fHourlyRate << " " << nNoofhoursPerWeek << " " << strEmployeeCategory << " " << fOvertime << " " << fTotalIncome << " " << fTax << " " << fNetIncome << " " << endl;
          empout.flush();
      }
         empout.close();
   }
       empin.close();
    return 0;
}

Recommended Answers

All 3 Replies

if you want to read all the lines in the file then make line 26 a while statement, not an if statement.

strEmployeeCategory == "J")

You can't do that with character arrays. You have to call strcmp() to compare two character arrays like this
if( strcmp(strEmployeeCategory, "j") == 0)

strcmp() returns 0 if the two strings are identical, or something else if they are not the same. If all you need to do is check the first character in strEmplooyeeCategory then you can use this instead:
if( strEmployeeCategory[0] == 'j')

@AD strEmployeeCategor is an STL string not a char[]. I think the issue he is having is that he is trying to compare floats and he has one of those .00000000000001's hanging around.

@AD strEmployeeCategor is an STL string not a char[].

You are right, I was looking at strEmployeeName

why is nNoofhoursPerWeek declared as a float instead of int?

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.