Hello, I am new to C++, this is only my second post on this forum.
Does any one know how to center text?
Thanks in Advance, August
P.S. My compiler doesn't have <windows.h>
Hello, I am new to C++, this is only my second post on this forum.
Does any one know how to center text?
Thanks in Advance, August
P.S. My compiler doesn't have <windows.h>
In a console window? Just pad with an appropriate number of leading spaces equal to half the screen width in characters minus half the length of the string you are trying to center.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I know how to do that allready.
I was just wandering if someone knew an eaiser way to do it.
i seem to recall there is a setw( ) function lying around somewhere, that might be handy but I dont remember a whole lot about it....
Couldn't think of anything interesting, cool or funny - sorry.
Try using
First u are setting a field of certain amt of spaces.Code:cout.width(#of characters that the console screen can hold); cout.setf(ios::center); cout<<"Hello World";
Then u re saying that u want to center the text
Then u print "Hello World" to the center.
I hope i am right cause it would be a shame to tell u incorrect info.
>I hope i am right cause it would be a shame to tell u incorrect info.
In that case you might want to consider refraining from further posts until you're sure of your answers. ios::center does not exist in standard C++. It may be an extension for your compiler, but it's not a common one. Fortunately, you can remove the call to setf completely and something like this would work:
Further tweaking should be obvious if needed.Code:#include <iomanip> #include <iostream> #include <string> using namespace std; int main() { string msg = "Hello, world!"; const int screen_width = 80; cout.width ( screen_width / 2 ); cout<< msg <<endl; }
My best code is written with the delete key.
Just a slight modification:
Code:cout.width((screen_width - msg.length()) / 2);
>Just a slight modification
aka. tweak, as in "Further tweaking should be obvious if needed."
My best code is written with the delete key.
It was needed . . . I just made it a little more obvious
Perhaps "if desired" would have been better wording.
My best code is written with the delete key.
no, i liked the first wording
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.