Thread
:
Need help with C++ please
View Single Post
Mentalist Traceur
2013-09-20 , 04:57
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
.
Quote & Reply
|
The Following 5 Users Say Thank You to Mentalist Traceur For This Useful Post:
bandora
,
Estel
,
MartinK
,
sixwheeledbeast
,
Wikiwide
Mentalist Traceur
View Public Profile
Send a private message to Mentalist Traceur
Find all posts by Mentalist Traceur