Need help with this Up Down homework problem.

Hey guys I need help with this homework problem.

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.

This is all I have so far. It keeps repeating for me and I can't get it to do what the problem asks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
	int count = 25;
	for (int i = 1; i< 26; i++){
		cout << "DOWN";

		cout << "UP";
		count--;
		Sleep(500);
	}

	system("pause");
	return 0;

}
closed account (48T7M4Gy)
Don't you need something like ncurses for this?

https://en.wikipedia.org/wiki/Ncurses
No I don't think you'd use ncurses for this
closed account (48T7M4Gy)
Yeah, there you go, I've never used it but had the impression it enabled you to print text anywhere on the console. But I'll take your word for it. UP's and DOWn's might have to sit tight til somebody else comes along.

@aqum

You need a function that can position the text on screen at your desired locations. Use a variable to position the "UP" word at the bottom of the screen, and subtract 1 from it during the for loop and using a different variable, position "Down" at the top. Add 1 to the variable each time. Don't forget to erase the text before increasing and decreasing each variable. Then cout the text at the new locations. I have a 'GotoXY' demo program that should help. Studying it should get you the help you need.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <conio.h> // Not everyone likes this. It works for me for these small programs
#include <iostream>
#include <windows.h>

using std::cout; // Instead of the whole 'using namespace std;'

void gotoXY(int x, int y); // Positions text to be printed
void WaitKey(); // A pause function

int main()
{
	int x;
	// Create a border
	for (x = 0; x < 80; x++)
	{
		gotoXY(x, 0);
		cout << "*";
		gotoXY(x, 23);
		cout << "*";
	}
	for (x = 1; x < 23; x++)
	{
		gotoXY(0, x);
		cout << "*";
		gotoXY(79, x);
		cout << "*";
	}
	int y = 5;
	// Start position of the text 

	gotoXY(24, 2); // Placing title
	cout << "GotoXY(x,y) Demonstration..";

	for (x = 1; x < 32; x++)  // Moves the text to the right
	{
		gotoXY(x, y);
		cout << "* column " << x << ", row " << y << "!";

		Sleep(200);
		gotoXY(x, y); // Locate the asterick
		cout << " "; // Remove it from screen, so we don't have a trail of astericks
	}
	x--; // Remove 1 from x, so we start where the text is now located

	for (y = 5; y < 22; y++) // Moves the text down the screen
	{
		Sleep(100);
		gotoXY(x, y);
		cout << "                                   "; // Remove the line of text
		Sleep(100);
		gotoXY(x, y + 1);
		cout << "* column " << x << ", row " << y + 1 << "!"; // Print new text line
	}
	Sleep(800);
	for (y = 22; y > 8; y--) // Moves text up the screen to row 8
	{
		Sleep(100);
		gotoXY(x, y);
		cout << "                                   ";
		Sleep(100);
		gotoXY(x, y - 1);
		cout << "* column " << x << ", row " << y - 1 << "!";
	}

	Sleep(1000); // Program finished, wait 1 second
	gotoXY(23, 22);
	cout << "Press any key to continue . . ."; // Let user know we're done
	gotoXY(23, 22); // Cursor to blink on the letter 'P' of aboves Press..
	WaitKey();  // Wait for a keypress
	return 0;
}

void gotoXY(int x, int y)
{
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD CursorPosition;
	CursorPosition.X = x; // Locates column
	CursorPosition.Y = y; // Locates Row
	SetConsoleCursorPosition(console, CursorPosition); // Sets position for next thing to be printed 
}

void WaitKey()
{
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
closed account (48T7M4Gy)
Use that, it works.
Thank you so much both of you
closed account (48T7M4Gy)
Pleasure aqum even though my effort was a red herring. Thks whitenite1 I've learned something.
@kemort

My pleasure also. With all that I've learned from this site, it feels good to give back.
Topic archived. No new replies allowed.