Thread: File I/O And IF Condition

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    39

    File I/O And IF Condition

    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.(it 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.

    Code:
    #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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So can you paste a few lines of "employees.txt"?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Another pointless waste of effort on a selfish cross-poster
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    It's all right i solved it.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    39
    And Salem maybe if you had given me a quick reply i wouldn't have had to "Cross Post?!"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe if you had provided sufficient information Salem could have given you a quick reply instead of asking for more information. Selfish cross-poster is selfish.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:25 AM
  2. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  3. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  4. pull out data frm a text file based on a condition
    By nirmala.s in forum C Programming
    Replies: 21
    Last Post: 11-27-2006, 12:56 AM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 09:44 PM