I need help trying to write the c++ code on the following algorithm.
Can anyone please help! I am totally confused on what to do.

// Set TotalTax to 0
// Set TotalGrossEarnings to 0
// Set TotalMedicalLevy to 0
// Set TotalEarnings to 0
// Display Heading
// Read Employee Record
// DOWHILE more records
// GrossEarnings = Hours * Rate
// TaxPayable = GrossEarnings * 0.15
// MedicalLevy = GrossEarnings * 0.01
// NetEarnings = GrossEarnings - TaxablePay - MedicalLevy
// TotalGrossEarnings = TotalGrossEarnings + GrossEarnings
// TotalTax = TotalTax + TaxPayable
// TotalMedicalLevy = TotalMedicalLevy + MedicalLevy
// TotalEarnings = TotalEarnings + NetEarnings
// Display EmployeeNumber, GrossEarnings, TaxPayable, MedicalLevy, NetEarnings
// Read Employee Record
// ENDDO
// Display TotalGrossEarnings
// Display TotalTax
// Display TotalMedicalLevy
// Display TotalNetEarnings

this is what I have for code so far, but I am finding many errors when I run & compile: Any suggestions

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

	using namespace std;
	int main()
	{
           // declare variables
           float totalTax = 0.0;
           float totalGrossEarnings = 0.00;
           float totalMedicalLevy = 0.0;
           float totalEarnings =0;
           string employeeRecord;
           int employeeNumber;
           int hours=0, rate=0;
           double grossEarnings (hours * rate);
           double taxPayable = grossEarnings * 0.15;
           double medicalLevy = grossEarings * 0.01;
           double netEarnings = grossEarnings - taxPayable - medicalLevy;
           double totalGrossEarnings = totalGrossEarnings + grossEarnings;
           double totalTax = totalTax + taxPayable;
           double totalMedicalLevy = totalMedicalLevy + medicalLevy;
           double totalEarnings = totalEarnings + netEarnings
 
           // declare input and output file streams
           ifstream inFile;
           ofstream outFile;

           // open the files for input and output
	   inFile.open("payin.txt", ios::in);
        outFile.open("payroll.dat", ios::out);

           // set format manipulators
         
	   cout << fixed << showpoint;
	   cout << setprecision(2);

	   // get input data from payin.txt
           inFile >> employeeNumber;
	       inFile >> hours;
           inFile >> rate;
	       infile >> totalTax;
           infile >> totalGrossEarnings;
           infile >>totalMedicalLevy;
           infile >>totalEarnings;

	   getline(inFile, employeeRecord);

           
           // Start Processing Loop

           while (infile)
           {
           // processing statements
           grossPay = hours * rate;
           taxPayable = grossEarnings * 0.15;
           medicalLevy = grossEarings * 0.01;
          netEarnings = grossEarnings - taxPayable - medicalLevy;
           totalGrossEarnings = totalGrossEarnings + grossEarnings;
          totalTax = totalTax + taxPayable;
           totalMedicalLevy = totalMedicalLevy + medicalLevy;
          totalEarnings = totalEarnings + netEarnings

          }      
          
           // decide how much payable tax & medical levy the employee should pay
           
           if (taxPayable >400.00)
               taxPayable = grossEarnings * 0.15;
           if (medicalLevy > 400.00)
               medicalLevy = grossEarings * 0.01;
           
           // calculate the net pay & total gross earnings
           ntEarnings = GrossEarnings - TaxablePay - MedicalLevy;
           totalGrossEarnings = totalGrossEarnings + grossEarnings;

           // display the result
           outFile << "employee Record for " << setw(40) << employeeNumber << endl;
           outFile << "Gross Earnings:" << setw(6) << grossEarnings << endl;
           outFile << "TaxPayable" << setw(6) << taxPayable< endl;
	       outFile << "Medical Levy" << setw(6) << medicalLevy << endl;
           outFile << "Net Earnings" << setw(6) << netEarnings << endl;

           // Read Next Record from the input file
	   // get input data from payin.txt
	   
	   inFile >> employeeRecord;
	   infile >> totalGrossEarnings;
       infile >> totalTax;
       infile >> totalMedicalLevy;
       infile >> totalNetEarnings;

	   getline(inFile, employeeRecord);
	   

           } // End of While loop
           // display the result
          
           outFile << "TotalGross Earnings:" << setw(6) << totalGrossEarnings << endl;
           outFile << "Total Tax" << setw(6) << totalTax< endl;
	       outFile << "Total Medical Levy" << setw(6) << totalMedicalLevy << endl;
           outFile << "Total Net Earnings" << setw(6) << totalNetEarnings << endl;

      	   inFile.close();
           outFile.close();
           
           system ("PAUSE");
           return 0;
	}

Edited 6 Years Ago by Nick Evan: Added code-tags

Any suggestions

Yes. Posting which errors you're getting might be a good start, don't you think?
And you should use code tags when you post code.

Ok iv only had a short look at this but looking at the while loop

while (infile)
{
// processing statements
grossPay = hours * rate;
taxPayable = grossEarnings * 0.15;
medicalLevy = grossEarings * 0.01;
netEarnings = grossEarnings - taxPayable - medicalLevy;
totalGrossEarnings = totalGrossEarnings + grossEarnings;
totalTax = totalTax + taxPayable;
totalMedicalLevy = totalMedicalLevy + medicalLevy;
totalEarnings = totalEarnings + netEarnings

}

how does that loop end? as loop condition (a file handle) isnt affected at all. You are doing maths operations which will never end the loop i dont think.

To me the loop should be something like the following

while(infile!=EOF)/*im not used to file streams in c++ but i use the EOF  marker for FILE* handles so this may need adjusted sorry*/
{
  //read a record from file

  //do your maths on the data

  //display the results 
 
  
}

Other than that i agree with the above we need some detail about the errors.

if you are having trouble with pseudo code try staying in pseudo code but refining it a time ot two more making it a little more like c++ for each iteration untill you are more comfortable making programs from it. This iterative approach whilst time consuming will help you see how to tackle a problem and each time you do it it gets easier :)

hope this helps

Edited 6 Years Ago by Kanoisa: n/a

here is another one that I am working that is not so long:
I am getting the following errors:
43 no match for 'operator<<' in 'infile << "employeeRecord[10]"'
54 `inFile' undeclared (first use this function)
55 `outFile' undeclared (first use this function)

#include <iostream>
#include <fstream>
#include <cstdlib>


 
 using namespace std; 
 
int main ()
{
     
    int employeeRecord[10];
    int hours_worked, hourly_rate_of_pay;
    float gross_pay;
    
    ifstream infile;
    infile.open("employeeRecord[10].txt",ios::in);
    ofstream outfile;
    outfile.open("employeeRecord[10].txt",ios::out);
    
     
    outfile <<"employeeRecord[10]"<<"employeeNumber"<<"hours_worked"<<"hourly_rate_of_pay"<<"gross_pay"<<endl;

    infile <<"employeeRecord[10]" <<"hours_worked"<<"hourly_rate_of_pay"<<"gross_pay"<<endl;
    while (!infile.fail())
{
    gross_pay = (hours * hourly_rate_of_pay);
        
   
     outfile  << "employeeNumber"<<"hours_worked"<<"hourly_rate_of_pay"<<"gross_pay"<<endl;
    
    infile <<"employeeRecord[10]"<<"hours_worked"<<"hourly_rate_of_pay"<<"gross_pay"<<endl;
   
  }
    inFile.close ();
    outFile.close (); 
    
    ("PAUSE");   
    return 0;
 }

Edited 6 Years Ago by Nick Evan: Added [CODE] ... [/CODE] tags

ok piece of cake here


"43 no match for 'operator<<' in 'infile << "employeeRecord[10]"'"

for this you are trying to write to an input stream, this is not allowed as its an instream


"54 `inFile' undeclared (first use this function)"

you have declared ifstream infile not inFile its case sensitive

"55 `outFile' undeclared (first use this function)"

as above

so, I should not be using ifstream inFile - what should I be using then?
thanks for your help!

well you have declared in the smaller code the ifstream as infile so just change the capital f so that the names match. also are you sure you want to write to the output before you read the input that seems backwards to me,... also

while (!infile.fail())

this im sure checks if opening the file worked or not so surely if the file opens correctly then this loop will not end im not 100% sure as i said i have little expierence with c++ streams im used to the traditional c way (yes i know im told its all wrong but im sticking to what i know untill im comfortable changing)

so for the while loop im going to sneekily steal from another thread here this is a part of a post from AncientDragon


A better way to code it is like this, which will exit the loop upon end-of-file and will not process the last item in the file twice.

while( fin >> next )
   {
      // blabla 

   }

thats how the people who use the file streams suggest it works. in the above snippet fin is an ifstream just like your infile so it would be

while(infile >> next)
{
  //your code here
}

hope this helps

This article has been dead for over six months. Start a new discussion instead.