View Single 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: