Computing.Net > Forums > Programming > Printing Alignment in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Printing Alignment in C

Reply to Message Icon

Name: J@MIE
Date: November 27, 2002 at 14:41:38 Pacific
OS: Win XP
CPU/Ram: 1.2Ghz / 512 SD
Comment:

First off I'd like to thank all the people who helped me with my last post... Thanks to u I got my program working, but now i have a different problem:

1) Is there a way to center words on the screen? [ex: printf("hello");] If i wanted "hello" centered, how would i do that?

2) Second, is there a way to pause a program for a certain amount of seconds, before it continues again?

Any help would be much appreciated...




Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 27, 2002 at 14:56:09 Pacific
Reply:

Remember typing class (I'm probably showing my age here) where you had to center your text by:

- determine length of text
- subtract length of text from length of line
- divide that number by 2
- type that many spaces
- then type text

You have to do the same thing, unless you can find a subroutine that does that for you. You'll have to calculate then output the correct number of spaces to cause your text to be centered (which implies that you know the number of characters of the output line).

I've done it before like:

int numSpaces = .....

printf("%*c%s\n",numSpaces,' ',outputText);


The * says to look at the next variable in the list (numSpaces) to determine how many positions this output field should use.

If your printf() was

printf("%10c%s\n",' ',outputText);

it would output 10 spaces then the contents of 'outputText', correct?

The first printf does the same, just the 10 isn't hardcoded into the printf(), but is obtained from 'numSpaces'.


0

Response Number 2
Name: aj67my
Date: November 30, 2002 at 23:23:14 Pacific
Reply:

=================
2) Second, is there a way to pause a program for a certain amount of seconds, before it continues again?

Yes, there are many ways. One is to include into your program, then call the function sleep(x); where x is the number of seconds to pause for. This does not work with all compilers, not mine anyway. If that doesnt work, you still have choices.

You can write your own function to pause for a certian amount of time:

#include
#include
#include

void sleep(int nbr_seconds ){
clock_t goal;
goal = (nbr_seconds * CLOCKS_PER_SEC) + clock();
while(goal > clock())
{
}
}

you could also use:
system("PAUSE");



0

Response Number 3
Name: aj67my
Date: November 30, 2002 at 23:24:26 Pacific
Reply:

dos.h is what you need to include for the pre made sleep function


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


display delay Help in C coding



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Printing Alignment in C

printing in C www.computing.net/answers/programming/printing-in-c/8814.html

Problem reading file in c++ www.computing.net/answers/programming/problem-reading-file-in-c/7826.html

ungetc in c++ www.computing.net/answers/programming/ungetc-in-c/8266.html