First compute the employee's gross pay. Gross pay is hours worked multiplied by hourly rate. We will assume (for
simplicity) that no employee will work more than 40.0 hours, therefore no overtime pay calculation is needed.
Next, compute the net pay. To do so, we must determine the amount of taxes to withhold. For simplicity we will
assume workers who make less than $300 have only 12% of their pay withheld for taxes. However, workers who
make at least $300, but less than $400 have 17% of their pay withheld for taxes. And finally, workers who make at
least $400 have 20% of their pay withheld for taxes.
You must have named constants for each of these dollar amounts and percentages, and you must use them in your
programming statements.
Finally, you must calculate these values for all the employees in the input file. Once you've computed all employees'
gross and net pays, print all data to the screen.

That the objective of the assignment, I was given an a example on how to display the output

CODE
Hours  Rate  Gross  (Tax %)  Net
-------------------------------
35.0   9.5     x            x        x

Im working it I have this incomplete code so far

CODE
//Reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const double THREE_HUNDRED_TAX = 12;
const double THREE_FOUR_HUNDRED_TAX = 17;
const double FOUR_HUNDRED_TAX = 20;
const int THREEHUNDRED = 300;
const int FOURHUNDRED = 400;


int main ()
{
    double hours1, rate1, hours2, rate2, hours3, rate3, hours4, rate4, hours5, rate5, gross1, gross2, gross3, gross4, gross5, net, tax, TaxHeld;
    ifstream inFile;
    
    inFile.open("pa3.input.txt");
    
    inFile >>hours1>>rate1>>hours2>>rate2>>hours3>>rate3>>hours4>>rate4>>hours5>>rate5;
  
    
    cout<< fixed << showpoint;
    cout<< setprecision(2);
    
    gross1=(hours1*rate1), gross2=(hours2*rate2), gross3=(hours3*rate3), gross4=(hours4*rate4), gross5=(hours5*rate5);
    
    if (gross1, gross2, gross3, gross4, gross5 < THREEHUNDRED)
               {
                              tax = THREE_HUNDRED_TAX;
                              TaxHeld = gross1*(THREE_HUNDRED_TAX /100);
                              net= TaxHeld - gross1;
                              cout<<net;
               }
    else if (gross1, gross2, gross3, gross4, gross5 >= THREEHUNDRED && gross1, gross2, gross3, gross4, gross5 < FOURHUNDRED)
                 {
                             tax = THREE_FOUR_HUNDRED_TAX;
                             }
    else
                 {
                             tax = FOUR_HUNDRED_TAX;
                 }
  
   inFile.close();
  


    system ("Pause Screen");
    return 0;
}

I dont like how its jumbled I want to clean it up I prefer it that way, also getting a bit stuck on how to format the output.

I am calling the input from a .txt file set up like this

35.0 9.75
40.0 10.25

The first hour is hours and second row is rate. I needed 5 lines of data, hence ten variable names hours1, rate1 and etc.

Also made constants for the tax % withheld

I am unsure how to formulate the if statement, I am sure I need one, to output it the way I show above.

Thanks, I just got a bit tired, not lazy.

The output should be pretty simple, the hardest part will be if you want to make it so it's a simple loop instead of a line per each thing

What I would do is set up the table how you want it, and then write the output function so that it determines how long the number is and adds the right amount of spaces after it so that it goes to the right spot before outputting the next piece.

Do you know about while loops, arrays, structs, and functions? And if so, are you allowed to use them in this? They would make your life so much easier cause you could use a struct to hold all Hours, Rate, Gross, Tax %, and Net for one employee in one variable, use an array to hold all those numbers, and then just cycle through with a loop that determines the gross, tax, and net for one and the outputs it before moving to the next employee and repeating in the array.

Actually, I can tell you exactly what to use to make it work. It's a bit annoying, but when you write out the top line of the chart (containing Hours, Rate, etc), count how many character make up in at the white space between in at the neighbor. In my example, there are 5 characters in "Hours" and I added 5 blanks after, thus giving me 10 character all together. I added the right amount of spaces after each so that they were all 10 characters long. Now, just write your cout line like this.

cout << left << setw(10) << hours1 << setw(10) << rate1 << /* rest of the line */;

The "left" tells the complier to left align the text, and then add the extra spaces after the output until it hits the end of the field (the setw(10)). Nice huh.

Just remember to add setw() before every variable or you'll have issues.

Hm we never got around to loops and arrays but if you post a quick tut then maybe I can learn

Also I see how you set it up but I am a bit confused with the if statement

If gross < 300 
     Tax = 12% 
Else If gross > 300 && gross <400
       Tax=17%
Else 
       Tax = 20%

Im just wondering who I pull out the "tax" which depends on the gross to calculate the rest.

Hours Rate Tax Gross Net
---------------------------------
x x x x x

The if statement you have is right, but you need to remember the ()'s and semicolons. From what you posted above, this is what I would do.

// below in a loop
if (gross < 300) 
     Tax = 12;
else if (gross >= 300 && gross < 400) 
     Tax=17;
else 
     Tax = 20;

TaxHeld = gross*(Tax /100);
net = gross - TaxHeld;

Do you have the updated code. If so, post it and I can tell you if it's looks right.

Well I have to read the input from a input file which I made in .txt file
She stated 5 lines of data with 2 columns on numbers. First column hours and second column rate, which I has able to call correctly doing

InFile>>hours1, rate1, and so on

Reading a text file:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

const double THREE_HUNDRED_TAX = 12;
const double THREE_FOUR_HUNDRED_TAX = 17;
const double FOUR_HUNDRED_TAX = 20;
const int THREEHUNDRED = 300;
const int FOURHUNDRED = 400;


int main ()
{
    double hours1, rate1, hours2, rate2, hours3, rate3, hours4, rate4, hours5, rate5, gross1, gross2, gross3, gross4, gross5, net, tax, TaxHeld;
    ifstream inFile;

    inFile.open("pa3.input.txt");

    inFile >>hours1>>rate1>>hours2>>rate2>>hours3>>rate3>>hours4>>rate4>>hours5>>rate5;


    cout<< fixed << showpoint;
    cout<< setprecision(2);

    gross1=(hours1*rate1), gross2=(hours2*rate2), gross3=(hours3*rate3), gross4=(hours4*rate4), gross5=(hours5*rate5);

    if (gross1, gross2, gross3, gross4, gross5 < THREEHUNDRED)
               {
                              tax = THREE_HUNDRED_TAX;
                              TaxHeld = gross1*(THREE_HUNDRED_TAX /100);
                              net= TaxHeld - gross1;
                              cout<<net;
               }
    else if (gross1, gross2, gross3, gross4, gross5 >= THREEHUNDRED && gross1, gross2, gross3, gross4, gross5 < FOURHUNDRED)
                 {
                             tax = THREE_FOUR_HUNDRED_TAX;
                             }
    else
                 {
                             tax = FOUR_HUNDRED_TAX;
                 }

   inFile.close();



    system ("Pause Screen");
    return 0;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Well, with what you have there, all that would be outputted is whatever is stored in net, and it would only do it once.

I'm not entirely sure how your code is suppose to work entirely, but this is what I think you need.

cout << fixed << showpoint << setprecision(2)
         << "Hours     Rate      Gross     Tax %     Net\n"
         << "--------------------------------------------------\n";

    for (int x = 0; x < 5; x++)
        { inFile >> hour >> rate;
          gross = hour * rate;
          
//        if ... tax = 12;
//        else if ... tax = 17;
//        else ... tax = 20;

//        calculate tax  
//        calculate net
          
          cout << left << setw(10) << hour << setw(10) << rate << setw(10) 
               << gross << setw(10) << tax << setw(10) << net << endl;
         }

Another thing you can do instead of the for line is just write while (inFile >> hour >> rate) and remove the line below it. The while way is how you would handle it if you didn't know how many lines of input were going to be given.

This is just me, because I've had enough teachers tell me to avoid using too many variables, but you can bring it down to 7 if you follow the example I have. However, if you want to keep it the way you have though, you'd have to have that if else chain 5 times like this. Plus, if you have more input, you'd need more variables and that would be a pain if you had 100 lines of input.

inFile >>hours1>>rate1>>hours2>>rate2>>hours3>>rate3>>hours4>>rate4>>hours5>>rate5;
    cout<< fixed << showpoint << setprecision(2)
         << "Hours     Rate      Gross     Tax %     Net\n"
         << "--------------------------------------------------\n";

    
    gross1=(hours1*rate1);
    gross2=(hours2*rate2);
    gross3=(hours3*rate3);
    gross4=(hours4*rate4);
    gross5=(hours5*rate5);

//        if (gross1 < 300) tax = 12;
//        else if (300 <= gross1 < 400) tax = 17;
//        else  tax = 20;
//        calculate tax  
//        calculate net
cout << left << setw(10) << hour1 << setw(10) << rate1 << setw(10) 
               << gross1 << setw(10) << tax1 << setw(10) << net1 << endl;

//       repeat "if through cout" for 2, 3, 4, and 5.
//       end program

If something doesn't make sense, feel free to ask.

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