Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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...

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 textYou 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'.

=================
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
#includevoid sleep(int nbr_seconds ){
clock_t goal;
goal = (nbr_seconds * CLOCKS_PER_SEC) + clock();
while(goal > clock())
{
}
}you could also use:
system("PAUSE");

![]() |
display delay
|
Help in C coding
|

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