Alex Rivera | Logout

Making a table with printf in c++

Asked 2012-02-09T07:00:33.263
9

I'm trying to make a table like this.... (Just without the dots I used to separate the each item)

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

This is what my code looks like for this part....

for (int count=0; count < numberOfEmployees; count++)
{
    cout << "Employee: \n";
    cout << "\t Name: ";
    cin.getline (employees[count].name, EMPLOYEESIZE); 

    cout << "\t Title: ";
    cin.getline (employees[count].title, EMPLOYEESIZE);

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

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

    cout << "\t Withholding Exemptions: ";
    cin >> employees[count].Withholding_Exemptions; 
    cin.ignore();

    cout << "\n";
}


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

cout << "\n";

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


for (int count=0; count < numberOfEmployees; count++)
{
    gross = employees[count].Salary;
    adjusted_income = subtraction_times (employees[count].Salary, employees[count].Withholding_Exemptions);
    tax = adjusted_income * .25;
    net = subtraction (gross, tax);

    printf ("\n%s", employees[count].name); 
}

I have the first part of the table(the name part), but after that I dont know now to do the rest of the table. Can anyone help me?

Thanks

Edit
Report

1 Answer

5

The most obvious question is: why use printf, when other tools are more adapt? Another question, often forgotten, is what is the (final) output medium? If the text is going to end up on a printer or in a text box in a windowing system, you may have your work cut out for you. The fonts on such systems are rarely fixed width, so you'll have to take into account the width of the individual characters. For output to a printer, I would suggest outputting LaTeX and then postprocessing it. For outputting to a window, the library you're using probably has some sort of table component which will do the formatting for you.

If you're outputing to some fixed width font device—a teletyp, for example, then you can either use iostream manipulators or user defined types. (There's no way to do this cleanly with printf—you need iostreams.) Abstractly speaking, defining types like Name, Title and MonitaryAmount is the cleanest solution. In which case, you just define an appropriate << operator for the type. Using a user defined type for name and title, instead of just std::string, may be overkill, however, and the manipulator approach may be preferred. (In a very large application, where the separate types would be justified, you're likely to need output in different contexts, and want manipulators to specify them as well.)

In the simplest solution, you could get by with just two manipulators: TextField and MoneyField: each manipulator would take the field width as an argument to the constructor, and set the appropriate format fields in its << operator, e.g.:

class TextField
{
    int myWidth;
public:
    TextField( int width ) : myWidth( width ) {}
    friend std::ostream&
    operator<<( std::ostream& dest, TextField const& manip )
    {
        dest.setf( std::ios_base::left, std::i
answered 2012-02-09T08:57:38.033

Your Answer