Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Centering The Text In A Simple C++ Program?
Dagoth Nereviar
post Nov 25 2006, 12:20 AM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 19-May 06
From: Leeds, UK
Member No.: 23,963



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:
QUOTE
Welcome! Please enter your first name.
__

And what's your second name?
__


But I want it to be like this:
QUOTE
Welcome! Please enter your first name.
__

And what's your second name?
__


My question is simply: how would I do it?

This post has been edited by Dagoth Nereviar: Nov 25 2006, 12:21 AM
Go to the top of the page
 
+Quote Post
osknockout
post Nov 25 2006, 03:38 PM
Post #2


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



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? biggrin.gif
Do tell if you have any problems.
Go to the top of the page
 
+Quote Post
Dagoth Nereviar
post Nov 25 2006, 04:07 PM
Post #3


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 19-May 06
From: Leeds, UK
Member No.: 23,963



Hahaha, thanks biggrin.gif

One quick question (I could probably work it out, but i'm lazy tongue.gif)...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++ laugh.gif )
Go to the top of the page
 
+Quote Post
Tsunami
post Nov 25 2006, 06:34 PM
Post #4


Premium Member
********

Group: Members
Posts: 172
Joined: 2-August 06
From: North Carolina
Member No.: 27,662



you can just use:
QUOTE

#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...
Go to the top of the page
 
+Quote Post
osknockout
post Nov 28 2006, 01:38 AM
Post #5


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



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. biggrin.gif
Go to the top of the page
 
+Quote Post
Dagoth Nereviar
post Nov 28 2006, 11:20 PM
Post #6


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 19-May 06
From: Leeds, UK
Member No.: 23,963



Thanks to both of you for the code smile.gif

I like the code Tsunami, but it might make the code waay to long tongue.gif Thanks though biggrin.gif

Osknockout, I think I get it tongue.gif 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 laugh.gif But thanks again for the help smile.gif
Go to the top of the page
 
+Quote Post
osknockout
post Nov 29 2006, 12:47 AM
Post #7


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



QUOTE
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. laugh.gif
In case you don't...
Go to the top of the page
 
+Quote Post
Dagoth Nereviar
post Nov 30 2006, 08:16 PM
Post #8


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 19-May 06
From: Leeds, UK
Member No.: 23,963



QUOTE(osknockout @ Nov 29 2006, 12:47 AM) *

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. laugh.gif
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 tongue.gif
Thanks, I don't have time right now (other college work >.<) but I should have looked over it/tried it by tomorrow tongue.gif

Thanks, from the big noob who thinks he knows it all rolleyes.gif laugh.gif
Go to the top of the page
 
+Quote Post
rize619
post Jul 6 2007, 06:45 PM
Post #9


Member [Level 1]
****

Group: Members
Posts: 66
Joined: 23-June 07
Member No.: 45,329



what about this function ?
apstring n2;
what does apstring do ?? please explain ?