Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,106 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,135 people online right now. Registration is fast and FREE... Join Now!





Scrolling text in console

Scrolls a line of text vertically on the console By Danny Battison

Submitted By: gabehabe
Actions:
Rating:
Views: 2,655

Language: C++

Last Modified: May 17, 2008
Instructions: WINDOWS SPECIFIC! (Makes use of Windows API)

A few of the functions, like gotoxy and clrscr, do NOT need to be defined if you are using Borland compiler (it already includes these functions) but I added them in this snippet in case you use a different compiler :)

Snippet


  1. /*****************************/
  2. /** VERTICAL SCROLLING TEXT **/
  3. /**    BY DANNY BATTISON    **/
  4. /**  gabehabe@hotmail.com   **/
  5. /*****************************/
  6.  
  7. #include <iostream>
  8. #include <windows.h>
  9. #include <time.h>
  10.  
  11. using namespace std;
  12.  
  13. void wait (int seconds); // timer
  14. void clrscr (); // clear the screen
  15. void gotoxy(int x, int y); // move to specific position in console
  16. void scroll (char *s, int x); // scroll a line of text
  17.  
  18. int main()
  19. {
  20.     // call our scroll function with the string and the y coordinate
  21.     scroll ("gabehabe", 35);
  22.     return 0;
  23. }
  24.  
  25. // wait a period of time
  26. void wait ( int seconds )
  27. {
  28.     clock_t endwait;
  29.     endwait = clock () + seconds * CLOCKS_PER_SEC ;
  30.     while (clock() < endwait) {}
  31. }
  32.  
  33. // clear the screen
  34. void clrscr ()
  35. {
  36.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); // gets the window handle
  37.     COORD coord = {0, 0}; // sets coordinates to 0,0
  38.     DWORD count;
  39.  
  40.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  41.     GetConsoleScreenBufferInfo(hStdOut, &csbi); // gets the buffer info (screen)
  42.  
  43.     // fill all characters as ' ' (empty the screen)
  44.     FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  45.  
  46.     // resets the cursor position
  47.     SetConsoleCursorPosition(hStdOut, coord);
  48. }
  49.  
  50. // move to a specific point in the console window
  51. void gotoxy (int x, int y)
  52. {
  53.     COORD coord; // coordinates
  54.     coord.X = x; coord.Y = y; // X and Y coordinates
  55.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
  56. }
  57.  
  58. void scroll (char *s, int x)
  59. {
  60.     // loops and leaves some space at the top/bottom border
  61.     for (int i = 19; i >= 5; i--)
  62.     {
  63.         gotoxy (x,i); // move up one line
  64.         cout << s; // output the string
  65.         wait(1); // wait one second
  66.         clrscr (); // clear the screen
  67.     }
  68. }

Copy & Paste


Comments


Codegamer 2008-08-29 12:05:23

That's the worst code I ever seen!!! I know a easier way to scroll text just with one little function.

dor1997 2008-09-18 09:05:17

Well, it's really too big, and you really can make it with only one short function, but it's still pretty cool. Good work!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month