Thread: Help with reading from a file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Post Help with reading from a file

    I'm trying to read in information from a .txt file. I have had a hard time finding the information on how to compile this correctly. The .exe will run but it seems to ignore the .txt file. I am compiling with gcc from the command line and using indirection to specify the input file (gcc source.cpp<payroll.txt). Do I have to specify the txt file too on the cmd line?

    The payroll.txt file just consists of 4 lines, each line has the hw and hr (hours worked and hourly rate).

    Here is the code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    const int MAX = 100;
        int n,hw[MAX],oth[MAX],rh[MAX];
        float hr[MAX], rp[MAX], otp[MAX], gp[MAX], tr[MAX], ta[MAX], np[MAX];
    /* Function Prototypes */
    int readData(int[],float[], const int);
    void getOTHours(int[],int[],int);
    void getOTPay(int[],float[],float[],int);
    void getRegularHours(int[],int[],int);
    void getRegularPay(int[],float[],float[],int);
    void getGrossPay(float[],float[],float[],int);
    void getTaxRate(float[],float[],int);
    void getTaxAmount(float[],float[],float[],int);
    void getNetPay(float[],float[],float[],int);
    void printAll(int[],float[],float[],float[],float[],float[],int);
    
    int main()
    {
    
    
        // Functions
        readData(hw,hr,MAX); // Grab Data
        getOTHours(hw,oth,n);
        getOTPay(oth,hr,otp,n);
        getRegularHours(hw,rh,n);
        getRegularPay(rh,rp,hr,n);
        getGrossPay(rp,otp,gp,n);
        getTaxRate(gp,tr,n);
        getTaxAmount(gp,ta,tr,n);
        getNetPay(gp,np,ta,n);
        printAll(hw,hr,otp,gp,ta,np,n);
    }
    
    //Function Defs
    int readData(int hw[], float hr[], int n){
        ifstream fin("payroll.txt");
        n = 0;
    
        while(fin>>hw[n]>>hr[n])
                n++;
                //fin.close;
                return n;
        }
    
    void getOTHours(int hw[],int oth[],int n){
        for(int i = 0;i < n;i++){
            if(hw[i] > 40) oth[i] = hw[i] - 40;
            else oth[i] = 0;
            }// end for loop
        }
    
    void getOTPay(int oth[], float hr[],float otp[], int n){
            for(int i = 0;i < n; i++){
                otp[i] = oth[i] * hr[i] * 1.5;
                }
        }
    
    void getRegularHours(int hw[],int rh[], int n){
            for(int i = 0;i < n; i++){
                if(hw[i] > 40)
                    rh[i] =  40;
                else rh[i] = hw[i];
                }
        }
    
    void getRegularPay(int rh[],float rp[],float hr[], int n){
            for(int i = 0;i < n; i++){
                rp[i] = rh[i] * hr[i];
                }
        }
    
    void getGrossPay(float rp[],float otp[], float gp[],int n){
            for(int i = 0;i < n; i++){
                gp[i] = rp[i] + otp[i];
                }
        }
    
    void getTaxRate(float gp[], float tr[], int n){
            for(int i = 0;i < n; i++){
                tr[i] = 0.10;
                }
        }
    
    void getTaxAmount(float gp[],float ta[],float tr[], int n){
            for(int i = 0;i < n; i++){
                ta[i] = gp[i] * tr[i];
                }
        }
    
    void getNetPay(float gp[], float np[], float ta[],int n){
            for(int i = 0;i < n; i++){
                np[i] = gp[i] - ta[i];
                }
        }
    
    void printAll(int hw[], float hr[], float otp[],float gp[], float ta[], float np[], int n){
        cout<<"Hours"<<"\t"<<"Rate"<<"\t"<<"OTP"<<"\t"<<"GP"<<"\t"<<"Tax"<<"\t"<<"Net"<<endl;
        for(int i = 0; i < n; i++){
        cout<<" "<<"\t"<<hw[i]<<"\t"<<hr[i]<<"\t"<<otp[i]<<"\t"<<gp[i]<<"\t"<<ta[i]<<"\t"<<np[i]<<endl;
        }
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    You need to actually open the file before reading it.

    First, define fin with "ifstream fin". Then, open the file. To do that, you use "fin.open ("payroll.txt"). When you're done reading all the information and don't need the file opened anymore, use fin.close ().

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    I did have fin defined already, so I added the statement to open the file. It still will only display the text I have outputting previous to using the info from the payroll.txt file. It doesn't error out or anything though.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    I didn't read the whole program but since you said you're just having problems with the file, I assume that's the only problem. So, is the file being opened? Add a condition to check if the file is open after you try to open it, and if it isn't display some error message so you can see if it's even opening the file.

    Remember your file has to be on the same folder as the exe and have the same exact name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM