Jump to content
xisto Community
Sign in to follow this  
Dagoth Nereviar

Centering The Text In A Simple C++ Program?

Recommended Posts

I'm currently learning C++ in college. For now it's very basic stuff (cout, cin, if, switch, voids, etc). But I'm also abit of a "must look neat" freak for certain things. One of which is that I don't like the text on the program being aligned to the right.

 

For example, I currently have something like this:

Welcome! Please enter your first name.

__

 

And what's your second name?

__


But I want it to be like this:

Welcome! Please enter your first name.

__

 

And what's your second name?

__


My question is simply: how would I do it?
Edited by Dagoth Nereviar (see edit history)

Share this post


Link to post
Share on other sites

Hmm... That would depend on how wide your window screen is - as in how many characters it will allow per line.
I refer you to this page.

The code on the page assumes that you use a standard 80 character wide line.
If you want to use a different one (for whatever reason) just change the '80' value
in the code and you'll be fine. Isn't it fun when you don't have to reinvent the wheel? :)
Do tell if you have any problems.

Share this post


Link to post
Share on other sites

Hahaha, thanks :)One quick question (I could probably work it out, but i'm lazy :rolleyes:)...Where would I put that? I mean, would I need it just the once or would I have to put it in each time? (I doubt the latter, but I'm a noob to C++ :rolleyes: )

Share this post


Link to post
Share on other sites

you can just use:

#include <conio.h>#include <IOSTREAM.h>
#include <bool.h>
#include <apstring.h>
#include <apstring.cpp>

void main()
{
clrscr();
gotoxy(22,1);
cout<<"Welcome! Please enter your first name.";
gotoxy(35,2);
apstring n1;
cin>>n1;
gotoxy(27,4);
cout<<"And whats your second name?";
gotoxy(35,5);
apstring n2;
cin>>n2;
clrscr();
gotoxy(35,1);
cout<<n1<<" "<<n2;
getch();
}


sample program attached...

Share this post


Link to post
Share on other sites

Lol. Basically you define it once and call it over and over again.take that example code from that site again.The function's centerstring right?You'd put the definition of the function anywhere [even outside int main()]-which is the code on the site- (even in a header file if you think you'll use it often enough)and call it whenever you wanted to, e.g. code...centerstring(something);...code.Nice code Tsunami, though I actually prefer just using strlen()and a bunch of manual stuff instead of adding (more like using) apstring. :)

Share this post


Link to post
Share on other sites

Thanks to both of you for the code :)

 

I like the code Tsunami, but it might make the code waay to long :rolleyes: Thanks though :)

 

Osknockout, I think I get it :huh: I know the call function bit, but would I put in the "centerstring([something])" bit? Would it be how many character there are?

 

Sorry for my noobness :D But thanks again for the help :rolleyes:

Share this post


Link to post
Share on other sites

Osknockout, I think I get it... but would I put in the "centerstring([something])

What type of noob asks and then says I know? Improper groveling I say...

If you look at the function again, you can tell that strlen() already computes the number of characters in your string for you. Actually, that would be your char pointer there Dagoth Nereviar, place your char or char array variable or whatever with characters you want to manipulate in that something. But of course, you already knew that. :)

In case you don't...

Share this post


Link to post
Share on other sites

What type of noob asks and then says I know? Improper groveling I say...

If you look at the function again, you can tell that strlen() already computes the number of characters in your string for you. Actually, that would be your char pointer there Dagoth Nereviar, place your char or char array variable or whatever with characters you want to manipulate in that something. But of course, you already knew that. :)

In case you don't...

 


Hehe, sorry, I wasn't saying I definatly knew, I was just guessing...And I told you, I am a noob :rolleyes:

Thanks, I don't have time right now (other college work >.<) but I should have looked over it/tried it by tomorrow :D

 

Thanks, from the big noob who thinks he knows it all :):rolleyes:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.