- Making an table with printf in c
- 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);
- }
- printf("%-10s", "title"); // this will left-align "title" in space of 10 characters
- #include <string>
- using namespace std;
- int main()
- {
- string name = "Bob Cratchit";
- string title = "Clerk";
- float gross = 15;
- float tax = 2;
- float net = 13;
- printf("%-25s%-20s%-10s%-10s%-10sn", "Name", "Title", "Gross", "Tax", "Net");
- printf("%-25s%-20s%-10.2f%-10.2f%-10.2fn", name.c_str(), title.c_str(), gross, tax, net);
- return 0;
- }
- Name Title Gross Tax Net
- Bob Cratchit Clerk 15.00 2.00 13.00
- std::cout << std::left << std::setw(25) << "Name" << std::setw(12) << "Title"
- << std::setw(11) << "Gross" << std::setw(9) << "Tax" << "Netn";
- 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::ios_base::adjustfield );
- dest.fill( ' ' );
- dest.width( manip.myWidth );
- return dest;
- }
- };
- class MoneyField
- {
- int myWidth;
- public:
- MoneyField( int width ) : myWidth( width ) {}
- friend std::ostream&
- operator<<( std::ostream& dest, MoneyField const& manip )
- {
- dest.setf( std::ios_base::right, std::ios_base::adjustfield );
- dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
- dest.fill( ' ' );
- dest.precision( 2 );
- dest.width( manip.myWidth );
- return dest;
- }
- };
- TextField name( 15 );
- TextField title( 8 );
- MoneyField gross( 8 );
- MoneyField tax( 6 );
- MoneyField net( 8 );
- for ( std::vector< Employee >::const_iterator employee = employees.begin();
- employee != employees.end();
- ++ employee ) {
- std::cout << name << employee->name()
- << title << employee->title()
- << gross << employee->salary()
- << tax << calculateTax( employee->salary() )
- << net << calculateNet( employee->salary() )
- << std::endl;
- }