Thread: ~ Array Help ~

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    ~ Array Help ~

    Hello again guys, I finished this program a week or two ago and it works like a million bucks now. Only problem is I need it to work w/ arrays. I don't really know much about arrays that is why I didn't write the program this way. Anyhow I am reading about arrays atm and I need to know how to read an arraay from an input file.

    Thanks for your help in advance :-)

    Here's my code:

    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<iomanip.h>
    
    int process_employee(int& empnum, float& emprate, float& emphours, ifstream& inData);
    void process_payroll(ifstream& inData, ofstream& outData, 
    					 const float& FEDERAL_TAX, 
    					 const float& STATE_TAX,
    					 const float& SOCSEC_TAX,
    					 float& totalGP, float& totalNP, float& totalFT, float& totalST,
    					 float& totalSS);
    void print_summary(ofstream& outData, float& totalGP, float& totalNP, 
    				  float& totalFT, float& totalST, float& totalSS);
    
    int main()
    {
    const float FEDERAL_TAX = .15; 
    const float STATE_TAX = .0524;
    const float SOCSEC_TAX = .07;
    
    float totalGP = 0, totalNP = 0, totalFT = 0, totalST = 0, totalSS = 0;
    char flag = ' ';
    int excesshours = 0;
    
    ifstream inData;
    ofstream outData;
    
    inData.open("A:\indata.txt", ios::in);
    outData.open("A:\outdata.txt", ios::out);
    
    process_payroll(inData, outData, FEDERAL_TAX, STATE_TAX, SOCSEC_TAX, totalGP, totalNP,
    				totalFT, totalST, totalSS);
    print_summary(outData, totalGP, totalNP, totalFT, totalST, totalSS);
    
    
    }
    
    int process_employee(int& empnum, float& emprate, float& emphours, ifstream& inData)
    {
      inData >> empnum;
      if(inData.eof()) return 0;
      
      inData >> emprate;
      if(inData.eof()) return 0;
    
      inData >> emphours;
      if(inData.eof()) return 0;
    
      return 1;
    }
    
    void process_payroll(ifstream& inData, ofstream& outData, 
    					 const float& FEDERAL_TAX, 
    					 const float& STATE_TAX,
    					 const float& SOCSEC_TAX,
    					 float& totalGP, float& totalNP, float& totalFT, float& totalST,
    					 float& totalSS)
    {
      int flag = 0;
      int empnum;
      float emprate, emphours, gross, paynet = 0, fed = 0, state = 0, socSec = 0;
      char rateType;
    
      outData << "Employee Payroll\n" << endl;
    
      outData << setw(10) << "Employee" << setw(10) << "Hours" << " " 
    	  << setw(10) << "Rate" << setw(10) << "Gross" << setw(10) << "Net"
           << setw(10) << "Fed"<< setw(10) <<  "State" << setw(10) << "Soc. Sec" << endl;
     
      flag = process_employee(empnum,emprate,emphours,inData);
      while(flag == 1)
      {
    	
    	  gross = (emprate * emphours);
    	  rateType = ' ';
    
    	  //$.15 bonus per hour for employees who work 35 hours or less
    	  if(emphours <= 35)
    	  {
    		  gross +=  .15 * emphours;
    		  rateType = '*';
    	  }
    	  
    	  if(emphours > 40)
    	  {
    		  //add overtime on top of regular wage
    			gross += (emprate * .5) * (emphours - 40);
    			rateType = '$';
    	  }
    
    	  fed = FEDERAL_TAX * gross;
    	  state = STATE_TAX * gross;
    	  socSec = SOCSEC_TAX * gross;
    
    	  paynet = gross - fed - state - socSec;
    
    	  //add up totals
    	  totalGP += gross;
    	  totalNP += paynet;
    	  totalFT += fed;
    	  totalST += state;
    	  totalSS += socSec;
    
    	  //print statements here for individual 
    
    	  outData <<  setw(10) << empnum << setw(10) << emphours << rateType 
    	   << setw(10) << emprate << setw(10) << gross << setw(10) << paynet
           << setw(10) << fed << setw(10) <<  state << setw(10) << socSec << endl; 
    
          flag = process_employee(empnum,emprate,emphours,inData);
    
      }
    }
    		  
    void print_summary(ofstream& outData, float& totalGP, float& totalNP, 
    				  float& totalFT, float& totalST, float& totalSS)
    {
      outData << "Summary-Totals for all employees\n" << endl;
    
      	  outData <<  setw(13) << "Gross Pay" << setw(13) << "Net Pay" << setw(13) 
    	  << "Fedral Tax" << setw(13) << "State Tax" << setw(13) << "Social Security" 
    	  << endl; 
    
    	  outData <<  setw(13) << totalGP << setw(13) << totalNP << setw(13) 
    	  << totalFT << setw(13) << totalST << setw(13) << totalSS
    	  << endl; 
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    arrays are very simple. to make an array, you start out like you would any other variable, except adding [ (size) ] on the end.

    Code:
       int x;   // < ~~~~ interger of x
      
       int x[10];  // <~~~ array of 10 x's
    by making an array, you dont have to do something like this:

    Code:
       int x_0;
       int x_1;
       int x_2;
       int x_3;
       int x_4;
       int x_5;
       int x_6;
       int x_7;
       int x_8;
       int x_9;
    note: notice how i went from 0-9 instead of 1-10. arrays start at the number 0 and end at 1 less than the size of the array. always.

    when you want to store a number to, say, the 3rd and 4th element of the x array, all you have to do is:

    Code:
        int x[10];
    
        x[2] = 4;
        x[3] = 2;
    and now if you did this:

    Code:
       x[4] = x[2] + x[3];
       cout << x[4];
    your output would be 6.

    you can also create multi-dimensioinal arrays like this:

    Code:
       int x[10][10];
    which creates a 2 dimensional array. a 2 dimensional array is similar to a grid or a tic-tac-toe board, where you put in 2 coordinates to get a value. you can have as many dimensions as you want (but dont go overboard with them as multi-dimensional arrays with too many dimensions tend to eat up memory).

    i didnt read your post to see how this would relate to your code, but i reccomend reading the tutorial section of this website, as it explains all of the basic concepts of the C++ language very well.
    I came up with a cool phrase to put down here, but i forgot it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM