0

I have to make a loop to gather all the information from the users input of the employees. As you can see, I have gotten the parts where I ask the user how many employees and what their information is. Now all I have to is print that information to the screen like this, just without the periods between each and with a few spaces between each :

Weekly Payroll:
Name...............Title........Gross.......Tax.........Net

----------------------------------------    

Ebenezer Scrooge.....................Partner...250.00......62.25.....187.75

Bob Cratchit...............................Clerk.......15.00........2.00.......13.00

And this is what I have :

#include <iostream>
using namespace std;


const int MAXSIZE = 20;


struct EmployeeT
{
    char name[MAXSIZE];
    char title;
    double SSNum;
    double Salary;
    double Withholding_Exemptions;
};

EmployeeT employees[MAXSIZE];

int main()
{

cout << "How many Employees? ";
int numberOfEmployees;
cin >> numberOfEmployees; 



    while(numberOfEmployees > MAXSIZE)
    {

            cout << "Error: Maximum number of employees is 20\n" ;
            cout << "How many Employees? ";
            cin >> numberOfEmployees; 
    }


            char name[MAXSIZE];
            int title;
            double SSNum;
            double Salary;
            double Withholding_Exemptions;


            for (int count=0; count < numberOfEmployees; count++)
            {
                cout << "Name: ";
                cin >> employees[ count ].name; 

                cout << "Title: ";
                cin >> employees[ count ].title;

                cout << "SSNum: \n";
                cin >> employees[ count ].SSNum;

                cout << "Salary: \n";
                cin >> employees[ count ].Salary;

                cout << "Withholding Exemptions: \n";
                cin >> employees[ count ].Withholding_Exemptions; 
            }

            double gross;
            double tax;
            double net;
            double adjusted_income;

            gross = employees[ count ].Salary;
            adjusted_income = employees[ count ].Salary - 1.00;
            tax = adjusted_income * .25;
            net = gross - tax;


            cout << "Weekly Payroll:\t Name \t Title \t Gross \t Tax \t Net \n";

            for (int count=0; count < numberOfEmployees; count++)
            {
                cout << employees[count].name << " \t" << employees[count].title << " \t" <<
                gross << "\t" << tax << "\t" << net << "\n";
            }
            system("pause");

}

Ok I updated the program. Now I'm trying to do the calculations. This what I'm doing...

To calculate payroll:

Gross pay is the weekly salary which was previously entered for the employee.

Net pay is calculated as the gross pay minus the amount of tax.

To calculate tax: Deduct $1 from the salary for each withholding exemption. This is the adjusted income. If the adjusted income is less than 0, then use 0 as the adjusted income.

Multiply the adjusted income by the tax rate, which you should assume is a flat 25%.

As an example, if Bob Cratchit has a weekly income of $15 and 7 dependents, then his adjusted income would be $8. His tax would be 25% of $8 which is $2, and therefore his net pay is $13.

I have started trying to get this. I put it between the second loop and the last loop. Is this right?

Gvegas222
  • 213
  • 2
  • 4
  • 11
  • 3
    Please be more specific. What exactly does not work. What have you tried? If you post code, reduce it to the minimum that is required to demonstrate your problem. For instance, since this question is about the output of your program, replace the part that takes input from the user with some hardcoded data, since it is not relevant. See http://sscce.org for more information on how to write good code-examples. – Björn Pollex Feb 01 '12 at 08:43

5 Answers5

1

A small example for printing columns using C++ streams:

#include <iomanip> 
#include <ios> 

std::ostream& operator<<(std::ostream& a_out, const EmployeeT& a_e)
{
    a_out << std::setfill(' ')
          << std::left
          << std::setw(sizeof(a_e.name))
          << a_e.name
          << std::setw(0)
          << a_e.title
          << "    "
          << std::setw(10)
          << a_e.SSNum
          << a_e.Salary
          << a_e.Withholding_Exemptions;

    return a_out;
}

This would allow you to write an employee to standard output using:

std::cout << employees[n] << "\n";

Use a similar approach for the column headings.

Other points:

  • Prefer std::string to char[] (or char*) then you don't have to limit the length of the string (or explicitly manage the memory)
  • Use std::vector instead of a fixed array to avoid a limit
  • Make use of STL algorithms (std::for_each, std::copy for example) for performing operations on elements in containers (or array)
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

You can use the '\t' sequence, which represent in C++ a "Tab space" (like when you press Tab on your Keyboard). As for the loop, it should be something like:

cout << "Weekly Payroll:\t Name \t Title \t Gross \t Tax \t Net \n";

for(int n=0; n < employees; n++)
{
cout << employees[n].name << " \t" << employees[n].title << " \t" ...... << "\n"; 
} 

It should work :)

CrAsHeR
  • 218
  • 3
  • 10
0

you can use the same loop for insertion and displaying or anything!! because it traverses through the whole loop.

cout << "Weekly Payroll:\t Name \t Title \t Gross \t Tax \t Net \n";

for (int count=0; count < numberOfEmployees; count++)
{
    cout << employees[count].name << " \t" << employees[count].title << " \t" <<
        Gross << "\t" << tax << "\t" << Net << "\n";
}

Find net,tax and gross of each structure object and print.

Check if you want name as int at the marked position.

#include <iostream> 
using namespace std;

const int MAXSIZE = 20;

struct EmployeeT {

    char name[MAXSIZE];
    char title;
    double SSNum;
    double Salary;
    double Withholding_Exemptions; 
};

EmployeeT employees[MAXSIZE];

int main() {

    cout << "How many Employees? ";
    int numberOfEmployees;
    cin >> numberOfEmployees; 

    while(numberOfEmployees > MAXSIZE) {

        cout << "Error: Maximum number of employees is 20\n" <<
            "How many Employees? ";
        cin >> numberOfEmployees;
    }

    int name;         // name is char[] or string
    int title;
    double SSNum;
    double Salary;
    double Withholding_Exemptions;

    for (int count = 0; count < numberOfEmployees; count++) {

        cout << "Name: \n";
        cin >> employees[ count ].name; 

        cout << "Title: \n";
        cin >> employees[ count ].title;

        cout << "SSNum: \n";
        cin >> employees[ count ].SSNum;

        cout << "Salary: \n";
        cin >> employees[ count ].Salary;

        cout << "Withholding Exemptions: ";
        cin >> employees[ count ].Withholding_Exemptions;
    }
}
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
0

You could look into the STL iomanip header functions, like setw() Formatting Cout Output in C++ using iomanip . With that it should be possible to get decent fix sized columns, which tabs likely wont.

Reonekot
  • 402
  • 4
  • 10
0

Your code has seems OK, although it is more complicated than it should be, and the input-loop has no error-handling (if a user enters something invalid, your program will stop working).

Here are some general pointers that can help you make your program better:

Community
  • 1
  • 1
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283