Active Topics

 


Reply
Thread Tools
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#1
Hey all,

I know this might sound silly... But I need help in a simple thing...

So the output of the code should look like this..

Code:
Dongxiao
Gross Amount: ............ $1000.00
Federal Tax: ............. $ 150.00
State Tax: ............... $  35.00
Social Security Tax: ..... $  57.50
Medicare/Medicaid Tax: ... $  27.50
Pension Plan: ............ $  50.00
Health Insurance: ........ $  75.00
Net Pay: ................. $ 605.00
Now I got the code to work to give me the above numbers.. But I don't know how to align it properly exactly like how it's shown above and I don't know how to add the "..." automatically as needed..

Here's my code so far:

Code:
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Experimenting with this, but didn't work out

//const char* text = "Constant text ";
//const size_t MAXWIDTH = 14;
//
//void print(const string& var_text, int num)
//{
//    cout << text
//              // align output to left, fill goes to right
//              << left << setw(MAXWIDTH) << setfill('.')
//              << var_text << ": " << num << '\n';
//}

int main()
{
	string name;
	int x;
	double FedIncTax, //Federal Income Tax
		StateTax, //State Tax
		SSTax, //Social Security Tax
		MedTax, // Medicare/Medicaid Tax
		PenPlan, //Pension Plan
		HealthIns = 75, //Health Insurance
		GrossAmnt, //Gross amount
		NetPay; //Net Pay
	
	//Input
	cout<<"Enter your name: ";
	cin>>name;
	cout<<"Enter your Gross amount: ";
	cin>>GrossAmnt;

	//Calculations
	FedIncTax = (GrossAmnt * 0.15);
	StateTax = (GrossAmnt * 0.035);
	SSTax = (GrossAmnt * 0.0575);
	MedTax = (GrossAmnt * 0.0275);
	PenPlan = (GrossAmnt * 0.05);
	NetPay = GrossAmnt - (FedIncTax + StateTax + SSTax + MedTax + PenPlan + HealthIns);
	
	//Output
	cout<< name <<endl;
	cout<<fixed<<setprecision(2);
	cout<<"Gross Amount: "<<setfill('.')<<setw(14)<<" $"<<GrossAmnt<<endl;
	cout<<"Federal Tax: "<<setfill('.')<<setw(15)<<" $"<<FedIncTax<<endl;
	cout<<"State Tax: "<<setfill('.')<<setw(17)<<" $"<<right<<StateTax<<endl;
	cout<<"Social Security Tax: "<<setfill('.')<<setw(7)<<" $"<<right<<SSTax<<endl;
	cout<<"Medicare/Medicaid Tax: "<<setfill('.')<<setw(5)<<" $"<<right<<MedTax<<endl;
	cout<<"Pension Plan: "<<setfill('.')<<setw(14)<<" $"<<right<<PenPlan<<endl;
	cout<<"Health Insurance: "<<setfill('.')<<setw(10)<<" $"<<right<<HealthIns<<endl;
	cout<<"Net Pay: "<<setfill('.')<<setw(19)<<" $"<<right<<NetPay<<endl;

	return 0;
}
Any help would be greatly appreciated!

Thanks in advance.

EDIT: And just so you can be filled in on what I'm talking about.. This is what the homework says..

(25 points) Write a program that calculates and prints the monthly paycheck for an employee. The net pay is calculated after taking the following deductions:
Federal Income Tax: 15%
State Tax: 3.5%
Social Security Tax: 5.75%
Medicare/Medicaid tax: 2.75%
Pension Plan: 5%
Health Insurance: $75.00
Your program should prompt the user to input the gross amount and the employee name. The output will be stored in a file. Format your output to have two decimal places. Your output should be formatted to two columns. The first column is left-justified, and the right column is right-justified.
EDIT: I updated the code and fixed the issue with the "...", but I have an issue with the alignment (especially with the result numbers)..
__________________
FarahFa.com

Last edited by bandora; 2013-09-20 at 04:48. Reason: Updated the code and fixed some of the issues..
 

The Following User Says Thank You to bandora For This Useful Post:
Posts: 1,048 | Thanked: 979 times | Joined on Mar 2008 @ SF Bay Area
#2
You don't have to be creative: You already have the correct number of "." characters in the sample output that you gave...

So:
Code:
cout << "Gross Amount: ............ $" << whatever << endl;
cout << "Federal Tax: ............. $" << whatever << endl;
__________________
qgvdial: Google Voice client. All downloads
qgvtp: Phone integration for the n900 that dials out and sends texts using qgvdial.
mosquitto: message broker that implements the MQ Telemetry Transport protocol version 3.
qgvnotify: Google voice and contacts notifier for diablo and maemo.

If you want to thank me, click the Thanks button.
If you'd like to thank my applications, vote to move them to extras.
 

The Following 2 Users Say Thank You to uvatbc For This Useful Post:
bandora's Avatar
Posts: 1,338 | Thanked: 1,055 times | Joined on Oct 2009 @ California, USA / Jordan
#3
Originally Posted by uvatbc View Post
You don't have to be creative: You already have the correct number of "." characters in the sample output that you gave...

So:
Code:
cout << "Gross Amount: ............ $" << whatever << endl;
cout << "Federal Tax: ............. $" << whatever << endl;
Thanks for your input, I've fixed that issue as you can see on the edited part of my post..

My only problem now is with the alignment.. (The numbers on the right has to be aligned to the right)..
__________________
FarahFa.com
 

The Following User Says Thank You to bandora For This Useful Post:
Posts: 2,225 | Thanked: 3,822 times | Joined on Jun 2010 @ Florida
#4
I'm not going to give you exact code, but I'll explain the logic (but you should know, if you intend to be a programmer, you'll want to get to a point where coming up with ways to do this stuff comes as second nature - I recommend you read each portion of what follows, and pause and see if you can figure out the rest, if you have the time):

Your output lines are of the format:
[string 1] [padding periods] [string 2]

So if you want every line to be the same number of characters, you want to count the length of [string 1] and [string 2], add those together, subtract that from your desired line width.

So:
[desired line width] - ([length of string 1] + [length of string 2])
or:
[desired line width] - [length of string 1] - [length of string 2]

That result number is the number of periods you want to insert (obviously subtract more for any other characters you want inserted, like spaces, if they aren't getting inserted as parts of string 1 or string 2).

Then you can just do a for-loop, printing one period in the loop body, and loop as many times as you have periods to print.

Alternatively, you can take the easy way out and use the stuff which comes in C++'s iostream and iomanip libraries to control the output of cout. (It has functions to set the width of the output, the fill/padding character, etc.) Consult the reference here (or another site more to your liking) for functions in the iostream/iomani library which look like they might be what you want (I think 'setw' is one of them):
http://www.cplusplus.com/reference/

[edit]
Ah, I see you're already experimenting with those iostream / iomanip approaches. I usually make my own logic instead of using them, as per the above approach, so I'm not be as good at spottng errors in their use, but what you've got commented out looks, at a glance, to be on the right track.
[/edit]

Also, usually sites like Stack Overflow often have answers available that explain stuff like this (though they frown on 'homework questions', they still have plenty that answer basic how-to's like this). In fact, a google for something like "c++ how to pad align output with filler padding character" will probably yield some clues.

Last edited by Mentalist Traceur; 2013-09-20 at 05:05.
 

The Following 5 Users Say Thank You to Mentalist Traceur For This Useful Post:
Posts: 2,225 | Thanked: 3,822 times | Joined on Jun 2010 @ Florida
#5
Oh, now if you are stuck trying to figure out what the length of "[string 2]" would be, since that's an integer/float/double variable, I would approach it like this:

You know that the amount of digits in the number determines the length.

You know in decimal counting systems, each digit is one greater multiple of 10, essentially.

For an integer, then, you can figure out how many digits it will be by dividing it by ten until it becomes zero (as C++ will floor decimal results for intergers).

For every power of ten it has, you add one more to your digits counter.

With a little bit of thinking, it should be possible for you to figure out how to convert the above into code. If you can't after a while, I can show you what I've written to do so. (Hint, either a loop, or a series of if/else-if checks with less/great -than comparisons.)

With a little more thinking, you can adapt such approaches to floats/doubles as well.

There's other ways too, of course, usually involving using other fancy C++ classes and stuff, if that's the route you want to go.
 

The Following 3 Users Say Thank You to Mentalist Traceur For This Useful Post:
bingomion's Avatar
Posts: 528 | Thanked: 345 times | Joined on Aug 2010 @ MLB.AU
#6
Hey.. that looks like homework!
Damn I wish I had the internet when i was studying
google printf string Padding

Edit: I never use C++ were C does the same thing.... I learnt C
C++ is bloated in some regards compared to C
bah anyway

Last edited by bingomion; 2013-09-20 at 11:44.
 

The Following 4 Users Say Thank You to bingomion For This Useful Post:
woody14619's Avatar
Posts: 1,455 | Thanked: 3,309 times | Joined on Dec 2009 @ Rochester, NY
#7
If you have a max expected width, say 6 digits:

printf("Gross Amount: ............ $%6.2f",value);

Nice thing about C++, you can still use C. Just saying.
__________________
Maemo Council Member: May 2012 - November 2012
Hildon Foundation founding member.
Hildon Foundation Board of Directors: March 2013 - Jan 15, 2014
 

The Following 4 Users Say Thank You to woody14619 For This Useful Post:
Posts: 2,225 | Thanked: 3,822 times | Joined on Jun 2010 @ Florida
#8
Oh yeah, printf has an option for padding the output too. (The problem with always figuring out the logic yourself is you sometimes completely forget avout some of the other options available that do the work for you, lol.) I still enourage learning programmers to figure stuff like that out. After all, someone has to know how to implement that logic, else who'll implement our printf() padding and our setw() and so on?

As for the comment about C++ being bloated, that really depends on what you mean by bloated, but for a lot of modern software need, you can easily end up with binaries that actually execute faster if you write it taking advantage of C++ features than using plain C. (And this is coming from a plain C fan, don't care for C++ much.)

Aside: Sometimes classes don't let you use the C stuff, and insist you use the C++ alternatives. I've had courses where the teacher demanded you used iostream and co. and nothing from stdio.h. I thought it was ******ed too.r
 

The Following 4 Users Say Thank You to Mentalist Traceur For This Useful Post:
Posts: 1,141 | Thanked: 781 times | Joined on Dec 2009 @ Magical Unicorn Land
#9
Your code is already very close.

You're using setprecision to control the number of digits on the dollar amounts, good. It might be good practice to save the previous precision in a variable, and then set it back to what it was when you're done, but if this is homework you may not have been taught that yet and strictly speaking it's irrelevant if this is the entire program in this particular case.

Since you are hardcoding the number of periods, instead of <<setfill('.') << setw(14) <<" $" you could do <<string(12,'.')<<" $" to print the periods. Or even better, just manually type them in the text like cout<<"Gross Amount: ............ $"

The numbers will actually be right-justified by default, so you don't need to include << right << in your couts. All you need now is another setw() before the numbers, to control the on-screen printed width of them. You'll also want to change the fill character to space again, instead of periods, so your numbers don't look like "$...123.45". If you avoid using setfill as mentioned in the previous paragraph, then you won't need to change that here since it'll already be a space by default, making the code more simple.

You could hardcode an setw value for the dollars, but ideally you could calculate the necessary width for the setw based on the input given. (so if I enter 123456789.12 it'll still align properly, and if I enter 123.45 it won't have a ton of extra spaces). But that might not be required for this exercise based on the wording of the question and depending on what the teacher has taught you so far.

Good luck and have fun
 

The Following 2 Users Say Thank You to stlpaul For This Useful Post:
bingomion's Avatar
Posts: 528 | Thanked: 345 times | Joined on Aug 2010 @ MLB.AU
#10
@Mentalist Traceur what i meant is there is over use of c++ obj orientation by c++ trained people.
Which is slower on the cpu ie games.
C people tend not to abuse it.
 

The Following User Says Thank You to bingomion For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 11:27.