Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.04 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Making an table with printf in c  
  2. for (int count=0; count < numberOfEmployees; count++)
  3. {
  4.     cout << "Employee: n";
  5.     cout << "t Name: ";
  6.     cin.getline (employees[count].name, EMPLOYEESIZE);
  7.  
  8.     cout << "t Title: ";
  9.     cin.getline (employees[count].title, EMPLOYEESIZE);
  10.  
  11.     cout << "t SSNum: ";
  12.     cin >> employees[count].SSNum;
  13.  
  14.     cout << "t Salary: ";
  15.     cin >> employees[count].Salary;
  16.  
  17.     cout << "t Withholding Exemptions: ";
  18.     cin >> employees[count].Withholding_Exemptions;
  19.     cin.ignore();
  20.  
  21.     cout << "n";
  22. }
  23.  
  24.  
  25. double gross;
  26. double tax;
  27. double net;
  28. double adjusted_income;
  29.  
  30. cout << "n";
  31.  
  32. cout << "Weekly Payroll: nName t t Title t Gross t Tax t Net n";
  33.  
  34.  
  35. for (int count=0; count < numberOfEmployees; count++)
  36. {
  37.     gross = employees[count].Salary;
  38.     adjusted_income = subtraction_times (employees[count].Salary, employees[count].Withholding_Exemptions);
  39.     tax = adjusted_income * .25;
  40.     net = subtraction (gross, tax);
  41.  
  42.     printf ("n%s", employees[count].name);
  43. }
  44.        
  45. printf("%-10s", "title"); // this will left-align "title" in space of 10 characters
  46.        
  47. #include <string>
  48. using namespace std;
  49.  
  50. int main()
  51. {
  52.     string name = "Bob Cratchit";
  53.     string title = "Clerk";
  54.     float gross = 15;
  55.     float tax = 2;
  56.     float net = 13;
  57.  
  58.     printf("%-25s%-20s%-10s%-10s%-10sn", "Name", "Title", "Gross", "Tax", "Net");
  59.     printf("%-25s%-20s%-10.2f%-10.2f%-10.2fn", name.c_str(), title.c_str(), gross, tax, net);
  60.     return 0;
  61. }
  62.        
  63. Name                     Title               Gross     Tax       Net
  64. Bob Cratchit             Clerk               15.00     2.00      13.00
  65.        
  66. std::cout << std::left << std::setw(25) << "Name" << std::setw(12) << "Title"
  67.           << std::setw(11) << "Gross" << std::setw(9) << "Tax" << "Netn";
  68.        
  69. class TextField
  70. {
  71.     int myWidth;
  72. public:
  73.     TextField( int width ) : myWidth( width ) {}
  74.     friend std::ostream&
  75.     operator<<( std::ostream& dest, TextField const& manip )
  76.     {
  77.         dest.setf( std::ios_base::left, std::ios_base::adjustfield );
  78.         dest.fill( ' ' );
  79.         dest.width( manip.myWidth );
  80.         return dest;
  81.     }
  82. };
  83.        
  84. class MoneyField
  85. {
  86.     int myWidth;
  87. public:
  88.     MoneyField( int width ) : myWidth( width ) {}
  89.     friend std::ostream&
  90.     operator<<( std::ostream& dest, MoneyField const& manip )
  91.     {
  92.         dest.setf( std::ios_base::right, std::ios_base::adjustfield );
  93.         dest.setf( std::ios_base::fixed, std::ios_base::floatfield );
  94.         dest.fill( ' ' );
  95.         dest.precision( 2 );
  96.         dest.width( manip.myWidth );
  97.         return dest;
  98.     }
  99. };
  100.        
  101. TextField  name( 15 );
  102. TextField  title( 8 );
  103. MoneyField gross( 8 );
  104. MoneyField tax( 6 );
  105. MoneyField net( 8 );
  106. for ( std::vector< Employee >::const_iterator employee = employees.begin();
  107.         employee != employees.end();
  108.         ++ employee ) {
  109.     std::cout << name  << employee->name()
  110.               << title << employee->title()
  111.               << gross << employee->salary()
  112.               << tax   << calculateTax( employee->salary() )
  113.               << net   << calculateNet( employee->salary() )
  114.               << std::endl;
  115. }