My first C++ class

Hello everyone!

I just started my first C++ class and have ran into a small but annoying problem with my first program. I have the logic down fine but I cannot for the life of me figure out why the very last line of the program will not format to the correct field width.
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
// ---------------------------------------------------------------
// Programming Assignment:	LAB1B
// Developer:			Jesse Wright
// Date Written:		8/29/11
// Purpose:		        Pay Calculator 
// ---------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;

void main ()

{
	double weeklySales;
	double grossPay;
	double fedTax;
	double socSecurity;
	double retirement;
	double totDeductions;
	double takeHomePay;

	cout<<"Please input the weekly pay for the employee and press the Enter key:";
	cin>>weeklySales;
	grossPay=weeklySales*.07;
	fedTax=grossPay*.18;
	socSecurity=grossPay*.06;
	retirement=grossPay*.1;
	totDeductions=fedTax+socSecurity+retirement;
	takeHomePay=grossPay-totDeductions;

	cout<<fixed<<setprecision (2)<<"Gross Pay (.07):\t $"<<setw(8)<<grossPay<<endl;
	cout<<"Federal Tax (.18):\t $"<<setw(8)<<fedTax<<endl;
	cout<<"Social Security (.06):\t $"<<setw(8)<<socSecurity<<endl;
	cout<<"Retirement (.1):\t $"<<setw(8)<<retirement<<endl;
	cout<<"Total Deductions:\t $"<<setw(8)<<totDeductions<<endl;
	cout<<"Take Home Pay:\t $"<<setw(8)<<takeHomePay<<endl;

}


I have a field width of 8 set on every line of output but when I run the program the "Take Home Pay" line is offset to the left of all of the others. If someone could please guide me in the right direction I would be very appreciative.
Last edited on
You'll have to add another tab for the "Take Home Pay" field. The widths of all the other fields are such that the next tab lines up for them.

Also, don't use void main(), use int main() instead and return 0; at the end of main.
I thought the same thing about void main() when my professor first had us use it. I have only seen int main() in everything I have done up until this point. It took me a second to realize that my programs then were not compiling because I had return 0; at the end of them. Why we have to do it this way is beyond me. Btw the extra tab worked (but you already knew that). Thank you!!
Returning an int with your main function is proper form and tells the CPU the program properly executed. Anything but 0 would indicate that an error occurred. Void is just saying your mainfunction doesn't return a value. It is strOngly suggested to return a value but is not required for your program to compile and execute (provided that your main function is set to void).
Unfortunately, if your professor tells you to use it, you may just have to go ahead and do it to pass the class. Just keep in mind that it is in fact nonstandard C++, and don't use it once you're out of the class.
It is strOngly suggested to return a value but is not required for your program to compile and execute (provided that your main function is set to void).


It's worth knowing that void main() is not C++. It's close, but the language definition is very clear on this. Many compilers let you get away with it, althought they really shouldn't.

However, if you use int main() and you don't return a value yourself, the compiler organises one for you (I think this is right, although I don't have a copy of the C++ standard to hand).
Topic archived. No new replies allowed.