Try the fastest way to create flashcards
Question

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

Verified
Answered 7 months ago
Step 1
1 of 4

This 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);		
	}}

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 by Godfrey Muganda, Judy Walters, Tony Gaddis

Starting Out with C++: Early Objects

8th EditionISBN: 9780133360929Godfrey Muganda, Judy Walters, Tony Gaddis
844 solutions
Fundamentals of Database Systems 7th Edition by Ramez Elmasri, Shamkant B. Navathe

Fundamentals of Database Systems

7th EditionISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
961 solutions
Introduction to Algorithms 3rd Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

3rd EditionISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
872 solutions
Introduction to Algorithms 4th Edition by Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen

Introduction to Algorithms

4th EditionISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
945 solutions

More related questions

1/4

1/7