Trying to format the data to be inline on the right side.

Hello, I'm just trying to format the output on the right to make it all inline. I am having a hard time getting that all aligned.


/**
* @file Salary.cpp
*
* @brief Computes employees pay period salary.
*
* This will compute the employees pay period salary
* with health insurance, FICA, and withholding
* taken out of the salary for each pay period.
*
* @author Spencer Phoenix
* @date 2021-10-14
*
*/

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cmath>

using namespace std;

/**
* @brief Computes employees pay period salary.
*
* This will compute the employees pay period salary
* with health insurance, FICA, and withholding
* taken out of the salary for each pay period.
*
* @return 0 is sucessful, non-0 on error
*
* Test 1:
* Enter the employee's name: Spencer Phoenix
* Enter the yearly salary: 75600
* Enter the number of pay periods: 6
* Enter the number of exemptions: 4
* Enter the yearly cost of the health insurance: 3680
*
* Employee: Spencer Phoenix
* Salary: 12600 (75600/yr)
* Pay Periods: 6
* Exemptions: 4
* Insurance: 613.33 (3860/yr)
* FICA: 781.20
* Withholding: 1449.00
* Net Pay: 9756.47
*
*
* Test 2:
* Enter the employee's name: John Penny
* Enter the yearly salary: 90000
* Enter the number of pay periods: 26
* Enter the number of exemptions: 6
* Enter the yearly cost of the health insurance: 13824
*
* Employee: John Penny
* Salary: 3461.54 (90000/yr)
* Pay Periods: 26
* Exemptions: 4
* Insurance: 531.69 (13834/yr)
* FICA: 214.62
* Withholding: 398.08
* Net Pay: 2317.15
*
*/

int main() {

string employeeName;
int yearlySalary;
int payPeriod;
int numExemptions;
double totalPayPeriod;
double healthInsurance;
double totalHealth;
const double FICA = 0.062;
double totalFICA;
const double taxWithHolding = 0.115;
double totalTaxHolding;
double netPay;
double totalTaxWithExemptions;
const int Exemptions = 15;
int totalExemptions;

//Enter the name of the employee
cout << "Enter the employee's name: ";
getline(cin, employeeName);

//Enter yearly salary
cout << "Enter yearly salary: ";
cin >> yearlySalary;
if (yearlySalary >= 0) {
yearlySalary = abs(yearlySalary);
}

//Enter number of pay periods for the year
cout << "Enter the number of pay periods: ";
cin >> payPeriod;
if (payPeriod >= 1) {
//Yearly salary / number of pay periods = totalPayPeriod
totalPayPeriod = static_cast<double>(yearlySalary) / payPeriod;
}

//Enter number of exemptions
cout << "Enter the number of exemptions: ";
cin >> numExemptions;
if (numExemptions >= 0) {
totalExemptions = numExemptions * Exemptions;
}

//Enter yearly health insurance cost
cout << "Enter the yearly cost of the health insurance: ";
cin >> healthInsurance;
if (healthInsurance >= 0) {
healthInsurance = abs(healthInsurance);
}

//yearly health insurance / number of pay periods = totalHealth
totalHealth = healthInsurance / payPeriod;

//FICA * totalPayPeriod = totalFICA
totalFICA = FICA * totalPayPeriod;

//taxWithHolding * totalPayPeriod = totalTaxHolding
totalTaxHolding = taxWithHolding * totalPayPeriod;

totalTaxWithExemptions = totalTaxHolding - totalExemptions;

//netPay = totalPayPeriod - totalHealth - totalFICA - totalTaxHolding
netPay = totalPayPeriod - totalHealth - totalFICA - totalTaxWithExemptions;

//Output must be displayed:
//Employees name
//Yearly salary
//Number of pay periods
//Number of exemptions
//Yearly cost of health insurance
//Salary for this pay period
//FICA tax amount
//Withholding amount
//Cost of the health insurance for this pay period
//Net salary for this pay period

cout << "Employee: " << employeeName << "\n";
cout << "Salary: " << right << setw(12) << setprecision(2) << fixed
<< totalPayPeriod << " (" << yearlySalary << "/yr)" << "\n";
cout << "Pay Periods: " << right << setw(12) << payPeriod << "\n";
cout << "Exemptions: " << right << setw(12) << numExemptions << "\n";
cout << "Insurance: " << right << setw(12) << setprecision(2) << fixed
<< totalHealth << " (" << setprecision(2) << fixed << healthInsurance << "/yr)" << "\n";
cout << "FICA: " << right << setw(12) << setprecision(2) << fixed << totalFICA << "\n";
cout << "Withholding: " << right << setw(12) << setprecision(2) << fixed << totalTaxWithExemptions << "\n";
cout << "Net Pay: " << right << setw(12) << setprecision(2) << fixed << netPay << "\n";

return 0;
}
Last edited on
Im having a hard time making the code tags.
Phoenix, it's broken when you make the original post, but edit your post and you'll be able to add code tags.

1
2
3
if (yearlySalary >= 0) {
yearlySalary = abs(yearlySalary);
}
What is the point of this?

1
2
3
4
if (payPeriod >= 1) {
//Yearly salary / number of pay periods = totalPayPeriod
totalPayPeriod = static_cast<double>(yearlySalary) / payPeriod;
}
What if payPeriod is not >= 1? totalPayPeriod will be left uninitialized. You should return from main early if there is invalid data, instead of printing junk results.
Last edited on
First, a short grammar lesson. (Sorry.)

“inline” is not the same as “in-line”

Inline means something is not separated out from the surrounding context. For example, an inline function is one where the compiler doesn’t actually create a function, but just puts the code that would have been in that function in the same place that function would have been called.

1
2
3
4
5
6
7
8
9
10
// What you write:

inline void hello() { std::cout << "Hello world!\n"; }

int main()
{
  std::cout << "Welcome to the show!\n";
  hello();
  std::cout << "That’s all folks!\n";
}
// What the compiler pretends you wrote:



int main()
{
  std::cout << "Welcome to the show!\n";
  std::cout << "Hello world!\n";
  std::cout << "That’s all folks!\n";
}


In-line means that something is lined-up in a straight line. You posted no example of what line you meant, but I will assume you want output like this:

Employee:            John Penny
Salary:      3461.54 (90000/yr)
Pay Periods:                 26
Exemptions:                   4
Insurance:    531.69 (13834/yr)
FICA:                    214.62
Withholding:             398.08
Net Pay:                2317.15


You cannot align multiple outputs as a single field. You need to use a stringstream to create a single output field, then align-print that.

 
#include <sstream> 
1
2
3
4
5
6
7
8
9
  cout << right << fixed;
  cout << "Employee: "    << setw(26) << employeeName << "\n";
  cout << "Salary:      " << setw(23) << (ostringstream{} << fixed << setprecision(2) << totalPayPeriod << " (" << yearlySalary << "/yr)").str() << "\n";
  cout << "Pay Periods: " << setw(23) << payPeriod << "\n";
  cout << "Exemptions:  " << setw(23) << numExemptions << "\n";
  cout << "Insurance:   " << setw(23) << (ostringstream{} << fixed << setprecision(2) << totalHealth << " (" << setprecision(0) << healthInsurance << "/yr)").str() << "\n";
  cout << "FICA:        " << setw(23) << setprecision(2) << totalFICA << "\n";
  cout << "Withholding: " << setw(23) << setprecision(2) << totalTaxWithExemptions << "\n";
  cout << "Net Pay:     " << setw(23) << setprecision(2) << netPay << "\n";

BTW, I presume this is a homework. If you are doing anything for actual employee salaries you should absolutely not be using floating point. Use integers for that data and only convert to float for printing output, as that will be exact:

1
2
3
4
5
unsigned long long insurance = 386000; // in pennies. Many places do it in hundredths of a penny
int payperiod = 4;
unsigned long long health = insurance / payperiod;

std::cout << "health = $" << (health / 100.0) << "\n";

Hope this helps.
The variable names are self-documenting which is excellent, so can get rid of all the garbage giving the following. Note the use of cout to get things lining up despite the mind-reading exercise required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    
    string employeeName;
    int yearlySalary;
    int payPeriod;
    int numExemptions;
    double totalPayPeriod;
    double healthInsurance;
    double totalHealth;
    const double FICA = 0.062;
    double totalFICA;
    const double taxWithHolding = 0.115;
    double totalTaxHolding;
    double netPay;
    double totalTaxWithExemptions;
    const int Exemptions = 15;
    int totalExemptions;
    
    cout
    << "          Enter the employee's name: ";
    getline(cin, employeeName);
    
    cout
    << "                Enter yearly salary: ";
    cin >> yearlySalary;
    if (yearlySalary >= 0) {
        yearlySalary = abs(yearlySalary);
    }
    
    cout
    << "    Enter the number of pay periods: ";
    cin >> payPeriod;
    if (payPeriod >= 1) {
        totalPayPeriod = static_cast<double>(yearlySalary) / payPeriod;
    }
    
    cout
    << "     Enter the number of exemptions: ";
    cin >> numExemptions;
    if (numExemptions >= 0) {
        totalExemptions = numExemptions * Exemptions;
    }
    
    //Enter yearly health insurance cost
    cout
    << "      Enter yearly health insurance: ";
    cin >> healthInsurance;
    if (healthInsurance >= 0) {
        healthInsurance = abs(healthInsurance);
    }
    
    //yearly health insurance / number of pay periods = totalHealth
    totalHealth = healthInsurance / payPeriod;
    
    totalFICA = FICA * totalPayPeriod;
    
    totalTaxHolding = taxWithHolding * totalPayPeriod;
    
    totalTaxWithExemptions = totalTaxHolding - totalExemptions;
    
    netPay = totalPayPeriod - totalHealth - totalFICA - totalTaxWithExemptions;
    
    
    // RESULTS
    cout
    << "   Employee: " << employeeName << '\n'
    << "     Salary: " << right << setw(12) << setprecision(2) << fixed
    << totalPayPeriod << " (" << yearlySalary << "/yr)" << '\n'
    << "Pay Periods: " << right << setw(12) << payPeriod << '\n'
    << " Exemptions: " << right << setw(12) << numExemptions << '\n'
    << "  Insurance: " << right << setw(12) << setprecision(2) << fixed
    << totalHealth << " (" << setprecision(2) << fixed << healthInsurance << "/yr)\n"
    << "       FICA: " << right << setw(12) << setprecision(2) << fixed << totalFICA << '\n'
    << "Withholding: " << right << setw(12) << setprecision(2) << fixed << totalTaxWithExemptions << '\n'
    << "    Net Pay: " << right << setw(12) << setprecision(2) << fixed << netPay << '\n';
    
    return 0;
}



          Enter the employee's name: Bertha Bumbag
                Enter yearly salary: 90000
    Enter the number of pay periods: 23
     Enter the number of exemptions: 2
      Enter yearly health insurance: 9000
   Employee: Bertha Bumbag
     Salary:      3913.04 (90000/yr)
Pay Periods:           23
 Exemptions:            2
  Insurance:       391.30 (9000.00/yr)
       FICA:       242.61
Withholding:       420.00
    Net Pay:      2859.13
Program ended with exit code: 0
PS some $ signs wouldn't go astray but that's a separate learning curve.challenge to lining things up - grammatically a line up (~line-up) in fact. ;)
More grammar:

Hyphenate compound adjectives, but leave the hyphen out otherwise.

  “We stood in line for hours.”

  “I have in-line skates.”

Ready for this?

  “Those words are not lined up.”

  “The lined-up words are beautiful.”

;-)


[edit] Forgot to underline the second example pair.
Last edited on
Ready for this?

At the considerable risk of being argumentative, a completely unknown cognitive state for @Duthomas, or me, 'in-line' sounds more like a brand name style and 'lined-up' as an accepted word appears to be a complete fiction along the lines of 'personal facts'.

Also, underlining didn't help me overcome those barriers.

In any case, is grammar really the relevant language technology here?
In any case, is grammar really the relevant language technology here?


No, I’m just a grammar nazi, and figured I'd try to educate you smurfs.



How’s that for an answer?
Please note that I do NOT think you are stupid in the very least.

Everything I bring to the table is for fun and enjoyment, and I find learning to be fun.
I did not make any of this grammarizing up. You can Google around “when to hyphenate words” if you want more. It’ll find you boring APA-style recommendations and other scholarly stuff, whereas I figure my pithy remarks ought to be sufficient for the armchair grammar nerd.

Sorry, use of the BS term “personal facts” set me off.
I’m calmer now.


[edit]
As a related off-topic aside: when I was learning Spanish some of the very best people I spoke with took the time to correct me and make me say things correctly. I know that a lot of people find that annoying, but I don’t actually care. I think it shows that said people cared about me.
Last edited on
Please note that I do NOT think you are stupid in the very least.
I know, but it wouldn't be the first time. Sometimes my own self-assessment has led me to that conclusion.

Sorry, use of the BS term “personal facts” set me off.
I’m calmer now.
Good. Why whisper with a small font? Celebrate your return to an unstressed state!

I did not make any of this grammarizing(sic) up.
Shouldn't that be grammarizing(sic)-up?
Last edited on
Shouldn't that be grammarizing(sic)-up?


LOL, no, it isn’t being used as an adjective.
And yes, it is a made-up word.


NB: “made-up” is being used as an adjective, modifying the word “word”.
Except, of course, in the last sentence, where it is not.
made-up-word?
grammarizing(sic) in that artificially constructed compound hyphenated word is a verb as is line in line-up? An adjective describes 'up'?
I have to go for a couple of hours unfortunately.
Foo.
I guess that ends this here.

Well, have fun where you go. :O)
I'm back. Now, where were we?
Topic archived. No new replies allowed.