Related questions with answers
Write a program that displays the word UP on the bottom line of the screen a couple of inches to the left of center and displays the word DOWN on the top line of the screen a couple of inches to the right of center. Moving about once a second, move the word UP up a line and the word DOWN down a line until UP disappears at the top of the screen and DOWN disappears at the bottom of the screen.
Solutions
VerifiedThis program exercise uses windows.h library to display "animation". We start by importing required libraries and declaring a namespace.
#include<iostream>
#include<windows.h>
using namespace std;
First we set word UP on the bottom line of the screen a couple of inches to the left of center.
int main()
{
//Declaration of HANDLE variable
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
//Start position of UP word
COORD posUp = {40, 50};
SetConsoleCursorPosition(screen, posUp);
cout <<"UP"<< endl;
Sleep(500);
Now we create an animation using a while loop until UP reaches top of the screen.
while (posUp.Y > 1)
{
// Erase characters at current position
// Move UP up one position, then pause
SetConsoleCursorPosition(screen, posUp);
cout << " " << endl;
posUp.Y--;
SetConsoleCursorPosition(screen, posUp) ;
cout << "UP" << endl;
Sleep(1000) ; //sleep for one second
}
Next, we are repeating the procedure for word DOWN. Only difference is the direction on Y axis
COORD posDown = {70, 5}; // Start position of DOWN
SetConsoleCursorPosition(screen, posDown);
cout <<"DOWN"<< endl;
Sleep(500);
while(posDown.Y< 50)
{
SetConsoleCursorPosition(screen, posDown) ;
cout << " " << endl;
posDown.Y++;
SetConsoleCursorPosition(screen, posDown);
cout << "DOWN" << endl;
Sleep(1000);
}}
#include <iostream>
#include <windows.h>
using namespace std;
void placeCursor(HANDLE, int, int);
int main()
{
/*--------output formatting variables------------*/
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
int rowUp = 25;
int rowDown = 0;
string up = "UP";
string down = "DOWN";
do
{
placeCursor(screen, rowUp, 5);
cout << up << endl;
placeCursor(screen, rowDown, 5);
cout << down << endl;
rowUp--; //decrementing the row so the UP can go down
rowDown++; //incrementing the row so the DOWN can go up
Sleep(100); //the screen output stops for 100 ms
system("cls"); //clears screen in each iteration
} while (rowUp > 0 && rowDown < 26); //when the rows are out of range
//the loop stops
return 0;
}
void placeCursor(HANDLE screen, int row, int col)
{
COORD position;
position.Y = row;
position.X = col;
SetConsoleCursorPosition(screen, position);
}
A typical text screen has 25 rows, or lines, with 80 print positions per row. The rows range from 0 to 24, with 0 being the top row of the screen. The print positions on each row, usually referred to as columns, range from 0 to 79, with 0 being at the far left-hand side. The row and column of a cell, which identifies its location on the screen, are called its coordinates.
In the code above, we have declared two variables for and which are used for changing the positions of the words vertically.
Create a free account to view solutions
Create a free account to view solutions
Recommended textbook solutions

Starting Out with C++: Early Objects
8th Edition•ISBN: 9780133360929Godfrey Muganda, Judy Walters, Tony Gaddis
Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
Introduction to Algorithms
3rd Edition•ISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
Introduction to Algorithms
4th Edition•ISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. CormenMore related questions
- computer science
1/4
- computer science
1/7