Need Help

Hi guys, I'm having trouble with my first assignment. I'm not asking for someone to just do the problem for me, but rather, I'd appreciate it if someone could point me in the right direction. Here's the assignment, and what I have so far. Thanks again.


Write a program to read salary data for an employee of a company and produce a report detailing the gross pay, deductions and net pay for each employee.

The deductions include

Federal income tax: 15%
State income tax: 5%
Payroll tax: 8.5%
Retirement: 8%
Health insurance: $750

The program should prompt the user for the employee's first name, last name and salary. Then it should calculate the deductions and net pay and produce a report like

Judy Garland
Gross pay: ............. $ 4000.00
Federal income tax: .... $ 600.00
State income tax: ...... $ 200.00
Payroll tax: ........... $ 340.00
Retirement: ............ $ 320.00
Health insurance: ...... $ 750.00
Net pay: ............... $ 1790.00

Notice that the numbers are right adjusted to get the decimal points aligned.



#include <iostream.h>
int main()
{

int name,salary,fed,state,payroll,retirement,netpay;
cout<<"Enter Name :";
cin>>name;
cout<<"Enter Salary :";
cin>>salary;
fed=salary*0.15;
state=salary*0.05;
payroll=salary*0.085;
retirement=salary*0.08;
netpay=salary - (fed) - (state) - (payroll) - (retirement);
cout<<"\n\nThe netpay is :";

system("pause");
return 0;
First thing is use code tags for your code... it makes it a lot easier to read. Second, you're using integers to store number that may possibly need decimal points. Look back at the different types of variables there are as well as how to format those types of values (setting the precision). Also remember to output every line item you have. Hopefully this points you in the right direction.
Okay, so instead of using "int", I should use "double" for fed, state, payroll, etc? Also, when you say output every line, do you mean "fed=salary*0.05;
cout>>fed;
And I don't want to sound annoying, but I'm not sure how to properly set the precision. This is the first program I've been assigned. I greatly appreciate your help though.
Yes, double or float would work. And yes, cout on those would output them to the screen so it would be produced like the report.

I'm still a beginner at this stuff too, and I've been teaching myself out of a book, so don't worry about asking too many questions. The people on this site have been extremely helpful to me and undoubtedly will be helpful to you as well.

As far as the setprecision, take a look at this page on the site: http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ once you read these type of documents a few times, it starts getting a lot easier to understand and helps reinforce your own knowledge if you do the research also. The documentation on this site is pretty good, and I generally go to it if I have problems ( afterwards I post questions :) ).

Also welcome to the forums!
Okay, still reading up on the precision area, but I made a few corrections. It's still compiling somewhat funky though aha. Also, I can't figure out why it correctly prompts for the name, but it won't prompt correctly for the salary.
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
#include <iostream.h>
int main()
{

int name,salary;
double fed,state,payroll,retirement,netpay;
cout<<"Enter Name :";
cin>>name;
cout<<"Enter Salary :";
cin>>salary;
fed=salary*0.15;
cout<<fed;
state=salary*0.05;
cout<<state;
payroll=salary*0.085;
cout<<payroll;
retirement=salary*0.08;
cout<<retirement;
netpay=salary - (fed) - (state) - (payroll) - (retirement);
cout<<netpay;

system("pause");
    return 0;

}
That maybe because you have the variable name setup as an integer right now. Also when you output the different lines (salary, state, payroll, etc.) it's going to show up all in one line all together, so your going have to space them out with cout << endl.

If you don't mind me asking, which compiler are you using? I went ahead and ran the code in VSE2010, so I ended up having to change a few things.
Oh okay, and I'm running bloodshed Dev-C++. And this is my progress so far. Made the changes, but I'm still not doing something right.
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
#include <iostream.h>

int main()
{

int name;
double salary,fed,state,payroll,retirement,netpay;

cout<<"Enter Name :";
cin>>name;
cout<<"Enter Salary :";
cin>>salary;
fed=salary*0.15;
cout<<fed << endl;
state=salary*0.05;
cout<<state << endl;
payroll=salary*0.085;
cout<<payroll << endl;
retirement=salary*0.08;
cout<<retirement << endl;
netpay=salary - (fed) - (state) - (payroll) - (retirement);
cout<<netpay << endl;

system("pause");
    return 0;

}

Last edited on
You're getting there though. You're going to have change int name to string name. You'll have to include the string header #include <string> as well. Then go ahead and take a look at this page so you can use getline http://www.cplusplus.com/reference/iostream/istream/getline/. Just follow the example after you go through it.
Slow but steady aha. Hmm, it won't compile for me now though. Do I use "getline" for salary as well?
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
#include <iostream.h>
#include <string> 



int main()
{
   char name[256];

string name;
double salary,fed,state,payroll,retirement,netpay;

cout<<"Enter Name :";
cin.getline (name,256);
cout<<"Enter Salary :";
cin>> salary;
fed=salary*0.15;
cout<<fed << endl;
state=salary*0.05;
cout<<state << endl;
payroll=salary*0.085;
cout<<payroll << endl;
retirement=salary*0.08;
cout<<retirement << endl;
netpay=salary - (fed) - (state) - (payroll) - (retirement);
cout<<netpay << endl;

system("pause");
    return 0;

}
No, you can only use std::getline() with std::strings. Note that std::cin.getline() is different than just std::getline().

You should also remove that char name[256] or std::string name; you can't have two variables with the same name.

Finally, Dev C++ is really old and outdated and has a lot of bugs. I'd suggest grabbing a newer IDE (there's an article around here somewhere with a list).
Aha, I really don't have time to start over on a new compiler for this program. It's due at 10 a.m. for my C++ class aha.
Can anyone help me finish this please? It's due in a few hours.
Add using namespace std; before line 6.
Remove line 10.

Line 1 should be #include <iostream>
Aha, I really don't have time to start over on a new compiler for this program


IDE, not compiler. It takes like 5 minutes (depending on your internet connection), the basics (setting up a project, setting up the files inside the project, compiling) are very similar between IDE's. If it's inconvenient for you right now, ok, but get rid of Dev C++ ASAP.
@Hanst99: That's right, it's IDE, not compiler. I think that was my bad since I asked earlier... it was time for my cat nap that late at night though (or morning, time is relative) :)

@ShaunL: Visual Studio Express has worked for me pretty good so far. I'm pretty sure there there are plenty of others to choose from so you may want to ask your instructor or browse the forums to see what everyone else uses.
Topic archived. No new replies allowed.