IF anyone can give me a little more assistance I would be very greatful.

I am working on this program for my C++ class and here is how it goes.

Directions.

Write a program that calculates and prints the monthly paycheck for an employee. The net pay is calculated after taking the following deductions.
Federal Tax 15%
State Tax 3.5%
Social Securtiy Tax 5.75%
Medicare 2.75%
Pension 5%
Health Ins $75.00
Your program shoud prompt user for input of employee name, and gross amount. The output will be stored into a file. format your output to have two decimal perscion.


As for when I run the program I type in the Employee name when prompted for name and then it prompts for Gross amount but doesnt wait for an answer.

Here is what I have so far:

string employee;
double gross;
double federal, state, ss, med, pension, health, net;

cout << "What is the Employees Name " << endl;
cin >> employee;
cout << "What is the Employees Gross Pay " << endl;
cin >> gross;

fstream fout;
fout.open ("taxes");
federal = (gross * .15);
fout << "Federal Tax" << federal << endl;
state = (gross * .035);
fout << "State Tax" << state << endl;
ss = (gross * .0575);
fout << "Social Security Tax" << ss << endl;
med = (gross * .0275);
fout << "Medicare/Medicaid Tax" << med << endl;
pension = (gross * .05);
fout << "Pension Plan" << pension << endl;
health = 75.00;
fout << " Health Insurance" << health << endl;
net = (gross - (federal + state + ss + med + pension + health));
fout << "Net Pay" << net << endl;

Not sure if I am on the write track but any help would be greatly appreciated.

Thanks ,
Alot
A

Dani AI

Generated

Diagnosis and short explanation (why the program seems to "skip" the gross prompt)
Operator>> stops at the first whitespace, so entering a multi-word name (for example "John Smith") leaves the remainder ("Smith") in the input buffer. The next extraction (cin >> gross) then tries to parse that leftover token as a number, fails, and sets the stream failbit — which makes it look like the program never waited. 's suggestion to use getline is correct; 's cin.get/char[] idea works but moves back to C-style strings; 's warning about get leaving delimiters is also valid. Prefer std::string + std::getline and validate numeric input.

Practical, robust pattern (read full name, validate gross, write nicely formatted output)
Use std::getline to capture the full name, validate gross with a loop that clears bad input, open an ofstream and check it, and apply std::fixed + std::setprecision(2) for two-decimal formatting. The snippet below shows the pattern (different from the code already in the thread):

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <limits>

int main() {
  std::string employeeName;
  double grossPay = 0.0;

  std::cout << "Employee name: ";
  std::getline(std::cin, employeeName);

  std::cout << "Gross pay: ";
  while (!(std::cin >> grossPay)) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Enter a numeric value for gross pay: ";
  }

  std::ofstream out("payroll.txt");
  if (!out) return 1; // handle file-open error

  out << std::fixed << std::setprecision(2);
  out << "Employee: " << employeeName << "\n\n";

  const std::vector<std::pair<std::string,double>> rates = {
    {"Federal Tax", 0.15}, {"State Tax", 0.035},
    {"Social Security", 0.0575}, {"Medicare", 0.0275},
    {"Pension", 0.05}
  };

  double net = grossPay;
  for (auto &r : rates) {
    double amt = grossPay * r.second;
    out << r.first << ": $" << amt << '\n';
    net -= amt;
  }
  double health = 75.00;
  out << "Health Insurance: $" << health << '\n';
  net -= health;
  out << "Net Pay: $" << net << '\n';
}

Other practical tips and cautions

  • Prefer std::ofstream for output-only files and give a clear filename like "payroll.txt". Check the stream after opening.
  • If mixing operator>> and getline in other orders, use std::cin.ignore(...) to discard the leftover newline.
  • Use std::fixed and std::setprecision(2) for two-decimal output.
  • Avoid C-style buffers unless there is a compelling reason; std::string plus getline is safer and simpler.

Recommended Answers

All 3 Replies

you are probably trying to enter a name that has embedded (white) spaces. to read an entire line (may contain white spaces) use getline.
getline( cin, employee ) ;

Yes, that is indeed the case. For cin , white space is a terminator, and the rest of the name stays in the input stream. the next cin statement reads from the remaining input buffer.
cin.get , will help u read the name inclusive of white spaces.
for
char name[20];
use
cin.get(name,19);

now it should work lets us know here.

>for
>char name[20];
>use
>cin.get(name,19);
He's already using std::string. Why are you suggesting that he downgrade to C-style strings? Oh, and you have an off-by-one logic bug. The size argument to get should be 20. Finally, you should recommend getline instead of get, because get has the potentially confusing feature of leaving the delimiter in the stream. In this case, unless you hit one of the less common terminating cases, you get the infamous scanf "bug":

#include <iostream>

int main()
{
  char buff[20];

  std::cin.get ( buff, 20 );
  std::cout<<'|'<< buff <<"|\n";
  std::cin.get ( buff, 20 );
  std::cout<<'|'<< buff <<"|\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.