Using the delay and sleep

Go down

Using the delay and sleep

Post  Quicksilver on Fri Aug 29, 2008 1:55 pm

      Most software developers seldom realise in their life time how vast the scope of C/C++. In this article (and the articles that I plan to post in the coming weeks we'll cover the functions avalable in C/C++ that are NOT TAUGHT in our classes; but which are a great way to get some extra marks in your practical exams (Trust me - I got 47/50 last sem and the external didn't even take my Viva! All thanks to this little function)

      This function has the ability to make a boring text output look a bit more attractive and all yor require is 5 more minutes than the usual amount you take to code!

Functions - delay () & sleep () - Both available in dos.h

function description -
      void delay (<int>) - Suspends program execution for <int> milliseconds.
      void sleep (<int>) - Suspends program execution for <int> seconds.

where to use this?
      where ever you want your output exection to halt for some fixed time. I can be used to create a rolling text effect as shown in the example code given below.

example code:

INSTRUCTIONS:
1. Copy the under-written code to a notepad document
2. Open the text file in C++ Editor (Tc.exe)
3. Save the file with .cpp extension
4. Run file to view output.

#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <dos.h>

void main()
{
char test [] = "This is a test string which explains the how the delay function works";

clrscr (); // Clearscreen Command

cout << "Seconds left: "; // * char preserves space character after :

for ( int i = 3; i >= 0; i-- )
{
sleep (1); // Suspends sys. op. for 1 sec
cout << "\b\b " << i; // "\b" moves cursor back 1 space
} // "\b " erases the last char in the current output line
cout << endl; // Endl = End line = "\n"

for ( i = 0; i <= strlen (test); i++ )
{
cout << test[i];
delay (50); // Suspends sys op for 3 milli sec
}
cout << endl;

cout << "\nPress any key to exit...\n";
getch ();
}


TOMORROW:

Working in Turbo C using a single header file for all functions!



PS: How was the article?

Regards,
Author    : Tridiv Sardesai a.k.a Quicksilver
Mail me at: tridiv.sardesai@gmail.com

_________________
The road to success is always under construction.
avatar
Quicksilver

Number of posts : 36
Age : 31
Location : Pune, Maharashtra, India
Branch : Computer Engineering
Registration date : 2008-08-08

View user profile

Back to top Go down

Re: Using the delay and sleep

Post  kanika on Mon Sep 22, 2008 8:26 am

hey it was a very good intro for the people in S.E those who dont know even 'C' of c language...and more than that it was much more info rather than which is learnt in any class...
it was good one and i think you can post more for the a bit higher knowledge of 'c' lang.
avatar
kanika

Number of posts : 2
Age : 28
Location : pune
Registration date : 2008-09-05

View user profile

Back to top Go down

Just a note.....

Post  Bluefire on Fri Sep 26, 2008 1:11 pm

well the informtaion posted above is for sure educative.........but the above explanation does not address the issue of printing console strings which have a "delayed" or a "roll-over" effect...... The solution that I came up involves a simple and neat function:


#include <stdio.h>
#include <conio.h>
#include <string.h> //for using the inbuild function strlen()
#include <dos.h>

#define MAX 50

void prn_my_string (char *my_str)
{
int i,j;
i = strlen (my_str);

for (j = 0; j < i; j++)
{
putch (*(my_str+j));
delay (50);
}

}
/*The above function takes a string as an i/p parameter and calculates its length using strlen() function and
stores it in the variable "i"...........and then the for-loop prints the string character wise with a roll-over effect to the o/p console*/

void main ()
{
char strbuf [MAX];
clrscr ();
printf("Press any key to begin the test...");
getch ();
printf("\n\n");
sprintf (strbuf, "The program implements a delay of %d secs", MAX);

/*sprintf is an inbuilt function which stores the string "The program implements a delay of %d secs" (as in this
case) directly into the string variable "strbuf" (as in this case) instead of printing it directly to the o/p
console.......PLEASE NOTE that the methodology of parameter passing in sprintf is exactly the same as in printf
other than the fact that one has to put in the string variable name(which stores the string)before entering the actual string*/


prn_my_string (strbuf); //the function call for delayed o/p of the string stored in "strbuf"
getch ();
}


comments appreciated.......

Bluefire

Number of posts : 1
Age : 29
Location : Pune
Registration date : 2008-08-24

View user profile

Back to top Go down

Somthing More Efficient..

Post  shingavi_s on Sat Oct 04, 2008 11:03 am

//For Roll over Of String

#include
#include
#include
#include

#define max 100
void main()
{
int i;
char a[max];
clrscr();
printf("Enter The String u want roll over = ");
gets(a); //Geting Input String
for(i=0;i {
printf("%c",a[i]);
delay(50);
}
getch();
}
avatar
shingavi_s

Number of posts : 19
Age : 29
Location : Pune-41
Registration date : 2008-08-25

View user profile http://www.sumitshingavi.wetpaint.com

Back to top Go down

Re: Using the delay and sleep

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum