read text file place in Arrays that'll be used in functions

I need to read from a text file, add that data to arrays, that use those arrays in functions that'll perform calculations, then create a new text file, and write calculation results to the new text file.

I can't figure out if I am placing data from the text file into the arrays. I think my problems start around line 120. The program will run and it will output. I have included many cout's to see where the compiler gets hung up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class Employee
{
public:
    
    void getInput( string    empLastName[]  , char payrollType[], double hoursWorked[],
                  double payRate[], int  unionCode[]  , int &count )
    {
        count = 0;
            cin >> empLastName[count];
            cin >> payrollType[count];
            cin >> hoursWorked[count];
            cin >> payRate[count];
            cin >> unionCode[count];
            count++;
    } // End getInput function.
    
    void doCalculations(
                        double hoursWorked[], double payRate[], int  unionCode[]  ,
                        double regularPay[],  double overtimePay[], double grossPay[],
                        double stateTax[],    double federalTax[],
                        double unionDues[],   double netPay[],
                        int count )
    {
        for( int i=0; i<count; i++ )
        {
            if( hoursWorked[i] > 40 )
            {
                regularPay[i]  = payRate[i] * 40;
                overtimePay[i] = (1.5*payRate[i]) * (hoursWorked[i] - 40.0);
            }
            else
            {
                regularPay[i]   = payRate[i] * hoursWorked[i];
                overtimePay[i] = 0;
            }
            grossPay[i] = regularPay[i] + overtimePay[i];
            
            // State and Federal tax
            if( grossPay[i] < 500 )
            {
                stateTax[i] = 0;
                federalTax[i] = 0;
            }
            else if( grossPay[i]<1000 )
            {
                stateTax[i]    = grossPay[i] * .03;
                federalTax[i]  = grossPay[i] * .05;
            }
            else
            {
                stateTax[i]   = grossPay[i] * .05;
                federalTax[i] = grossPay[i] * .07;
            }
            if( unionCode[i] == 1 ) unionDues[i] = 15;
            else if( unionCode[i] == 2 ) unionDues[i] = 25;
            else                         unionDues[i] = 35;
            
            netPay[i] = grossPay[i] - stateTax[i] - federalTax[i] - unionDues[i];
        }
    } //  End doCalculations function
};



const int SIZE = 5;

void outputLine ( string    empLastName[]  , char payrollType[], double hoursWorked[],
                 double payRate[], int  unionCode[]  );

int main(int argc, const char * argv[])
{
    
    /////////////////////////////////
    //Read an external file from precise location
    /////////////////////////////////
    
  string getcontent;
  ifstream inClientFile ("/Users/joshsmith01/Documents/Xcode Projects/for testing purposes/for testing purposes/employee_data_in.txt");
  
  
  if (!inClientFile)  // If statement tests wheter or not the file was in fact opened and displays the appropriate output.
  {
    cerr << "File could not be opened" << endl;  // Test to see where xcode gets past.
    exit(1);
  }
  else
  {
    cout << "File reading was successful" << endl;  // Test to see where xcode gets past.
  }
  
  
  if (inClientFile.is_open())
  {
    while (!inClientFile.eof())
    {
      inClientFile >> getcontent;
      cout << getcontent << endl;  // Test to see where xcode gets past.
    }
  }
    Employee worker;
    //  number of items in the array
    int count = 0;
    
    // Declare Array Variables for input by user
    string empLastName[SIZE];
    char   payrollType[SIZE] ;
    double hoursWorked[SIZE];
    double payRate[SIZE];
    int    unionCode[SIZE];
    
    // Declare Array Variables calculated
    double regularPay[SIZE];
    double overtimePay[SIZE];
    double grossPay[SIZE];
    double stateTax[SIZE];
    double federalTax[SIZE];
    double unionDues[SIZE];
    double netPay[SIZE];

    
    
    cout << "Right before While statement\n";  // Test to see where xcode gets past. 
    while ( inClientFile >> empLastName[SIZE] >> payrollType[SIZE] >> hoursWorked[SIZE] >> payRate[SIZE] >> unionCode[SIZE] ) {
        cout << empLastName[SIZE] << " " <<  payrollType[SIZE] << " " <<  hoursWorked[SIZE]  << " " <<  payRate[SIZE] << " " <<  unionCode[SIZE] << endl;
    
    
    // 11:23 PM03Nov2012 I don't think the employee_data_in.txt file is being read *correctly*. I think it is being read.
  
  cout << "right before object.functions\n";  // Test to see where xcode gets past.
    worker.getInput( empLastName, payrollType, hoursWorked, payRate, unionCode, count );
        
        
    
    worker.doCalculations( hoursWorked, payRate, unionCode,
                   regularPay, overtimePay, grossPay,    stateTax,
                   federalTax, unionDues,   netPay,
                   count );
    }
    cout << hoursWorked[SIZE] << endl;
    cout << "right after object.functions.\n";  // Test to see where xcode gets past.
    /////////////////////////////////
    //Write to a new external file //
    /////////////////////////////////

    
    ofstream outClientFile( "/Users/joshsmith01/Desktop/clients.txt", ios::out );  // Opens the new file to display the results of
    // calculations in the form of Last Name, Gross Pay, and Net Pay only.
    // ~ (tildé) is the short cut for the current user's user folder.
    
    if (! outClientFile )
    {
        cerr << "File could not be opened" << endl; // Tests to see if the new file has in fact opened. 
        exit( 1 );
    }// end if
    else
    {
        cout << "File outClientFile.txt creation was successful"<< endl;  
    }

//     while ( cin >> empLastName[SIZE]  >> grossPay[SIZE] >> netPay[SIZE] ) {
        outClientFile << empLastName[SIZE] << ' ' << grossPay[SIZE] << ' ' << netPay[SIZE] << endl;
        cout << "????";
    
    system("pause");
    return 0;
};
Last edited on
I don't think you need line# 81-89 because your checking at line 92.
Just move the else statement

I'm new to C++ too and can't compile your code to test because it's not complete, but I think your on the right path by using cout to step though the process and see where the problem lies.

It would also be helpful to me, if you include a few lines from your input txt file for reference.


I rewrote part of your code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Search file for String */

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int offset;
int counter;
string getcontent;
ifstream inClientFile;

int main(int argc, const char * argv[])
{
    
    /////////////////////////////////
    //Read an external file from precise location
    /////////////////////////////////
    
  string getcontent;
  ifstream inClientFile ("/temp/out1.txt");
  
/*  moved error
  if (!inClientFile)  // If statement tests wheter or not the file was in fact opened and displays the appropriate output.
  {
    cerr << "File could not be opened" << endl;  // Test to see where xcode gets past.
    exit(1);
  }
  else
  {
    cout << "File reading was successful" << endl;  // Test to see where xcode gets past.
  }
*/  
  
  if (inClientFile.is_open())
  {
    while (!inClientFile.eof())
    {
      inClientFile >> getcontent;
      cout << getcontent << endl;  // Test to see where xcode gets past.
    } // End While
  }// end if
  else
    {
    cerr << "File could not be opened" << endl;  // Test to see where xcode gets past.
    exit(1);
    }

  return 0;
}


I can't get the code to work after that, looks like getcontent is good but I don't see where your putting getcontent to any of your array values. So I would say that is what you need to do next.
Topic archived. No new replies allowed.