Like Tree5Likes
  • 1 Post By Salem
  • 1 Post By rags_to_riches
  • 3 Post By laserlight

File I/O And IF Condition

This is a discussion on File I/O And IF Condition within the C++ Programming forums, part of the General Programming Boards category; read from a file okay for each line i save the respective data into their respective variables i output each ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    So can you paste a few lines of "employees.txt"?
    twomers likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Salem likes this.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,660
    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.
    Salem, nvoigt and jimblumberg like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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-26-2006, 11:56 PM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 08:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21