-
January 31st, 2012, 12:16 AM
#1
Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?
Hello All,
My question is simply how do I center the lines on top of one another using setw (without the use of an additional header file or an additional function)?
I've attempted to no avail as well.
I can manually set the numbers of setw of course, but I was hoping for alternative smarter fix (without having to pain staking manually set & test each setw).
Note for reason I cannot use an additional header file or function: I've always wanted to know if this could even be done without the use of the <strings> header file (and without the use of an accompanying function). That, and I wanted to reduce the amount of lines of code if possible. If there's not an easier way that's fine (just wanted to know if setw or setfill could get the centering job done alone).
I didn't think it could be done (I believe with the header files I have and place & with no additional functional help one would have to simply manually set each setw).
This one has always intrigued me. As always any help is appreciated.
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
char Enter;
cout << setw(0) << "FIRST LINE HAS 28 CHARACTERS" << endl << endl;
cout << setw(0) << "SECOND LINE HAS 18" << endl << endl;
cout << setw(0) << "THIRD LINE HAS 28 CHARACTERS" << endl << endl;
cout << setw(0) << "FOURTH LINE HAS 18";
cin.ignore();
cin.get(Enter);
return 0;
}
-
January 31st, 2012, 02:39 AM
#2
Re: Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?
I too have my doubts. It would be easier to just write that function with the help of std::string anyway.
-
January 31st, 2012, 04:23 AM
#3
Re: Centering Text With SetW Or SetFill Without The Use Of <Strings> Or Function?
Originally Posted by Alyssa Saila
I've always wanted to know if this could even be done without the use of the <strings> header file (and without the use of an accompanying function). That, and I wanted to reduce the amount of lines of code if possible. If there's not an easier way that's fine (just wanted to know if setw or setfill could get the centering job done alone).
Somehow you need to know the length of the string. How are you going to accomplish that without calling some function?
Just write a function using std::string. That in itself reduces the number of lines of code in your main() program and makes your program more readable anyway.
Code:
void WriteCenteredString( int nTotalLineWidth, const std::string& strToCenter)
{
}
Then you just call this function any number of times you want with any string you want, with any total line length you wish.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 31st, 2012 at 04:28 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|