Confusion on windows library

closed account (3voN6Up4)
Hey guys, I have a homework assignment that I am stuck on. Here is the 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.

It's confusing because in this chapter they are talking about the windows.h library, screen control, etc. Some things they use in example programs are;

1
2
3
4
5
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

COORD position; 
position.X = 38;
position.Y = 11;


My teacher said to make a class or structure and simply just input blank lines. I re-read the book and there are some strange functions relating to these types of programs. I'm not too sure of what to do. This is what I have so far:
1
2
3
4
5
6
7
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
    cout << "DOWN\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nUP" << endl;
}


I apologize for the lack of code, I just have no idea where to start! :/
Last edited on
closed account (3voN6Up4)
Maybe I have to use the COORD position for an std cout? I don't know how to put it as code though
You may use the windows function SetConsoleCursorPosition()
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025%28v=vs.85%29.aspx

It is often convenient to use that within your own function such as the gotoxy() example in this thread:
http://www.cplusplus.com/forum/beginner/4234/#msg18563

closed account (3voN6Up4)
I still don't get it :/ how is that windows function used? I'm a newbie for this kind of stuff.
Something like
1
2
3
4
5
6
7
8
9
for(int K=0; K<screen_height; ++K){
   clear_screen();
   gotoxy(center+2in, K);
   output( "DOWN" );
   gotoxy(center-2in, screen_heigth-K-1);
   output( "UP" );

   delay(1s);
}
you are going to fight with specific functions to handle the Windows console. If you are in a beginner programming course, I think that exercise would be a source of headaches and a waste of time.

Instead, follow the advice of your teacher and do something like
1
2
3
4
5
6
7
8
9
for(int K=0; K<height; ++K){
   print_n_line_breaks(K);
   std::cout << "Down\n";
   print_n_line_breaks(/**/); //separation between Up and Down.
   std::cout << "Up\n";
   print_n_line_breaks(K);

   std::cin.get(); //press enter (and only that) to continue
}
where print_n_line_breaks() can be done with cout and '\n'
Last edited on
closed account (3voN6Up4)
Ah okay, so do you mean something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
    for (int k = 0; k < height; ++k) //++k is the same as k++ right?
        {
            cout << k << endl; //I prefer endl than doing \n, if that is alright.
            cout << "DOWN" << endl;
            cout << "----" << endl;
            cout << "UP" << endl;
            cout << k << endl;

            cin.get();
        }
}


The code you use is definately different from what I use, just trying to clarify that I translated it into what I believe it is. What is height though?
closed account (3voN6Up4)
Also, I don't know why it is asking to display UP/DOWN "a couple inches to the left of the center" are you supposed to just judge it for yourself?
> cout << k << endl;
if K is 3, that would output a 3.
I meant to output 3 line breaks, like cout << '\n' << '\n' << '\n';
you may use a loop, with K as the limit, for that.

> cout << "----" << endl;
the idea was to put a variable number of line breaks between "Up" and "Down", so they get closer in each iteration.
Just realize that in this way, "Up" would never be above "Down". Stop the loop when they are next to each other, then you may do another code for the words to separate.

> What is height though?
an arbitrary number representing the screen height. Just put 13 or something, there is no need to get the precise number.

> why it is asking to display UP/DOWN "a couple inches to the left of the center"
so they don't overlap when they cross, but that's irrelevant now.

> I prefer endl than doing \n, if that is alright.
Sure, there are some differences, but that's not important.
closed account (3voN6Up4)
Alright thanks, I sort of see what I am supposed to be doing, also is ++k the same as k++?
Last edited on
Topic archived. No new replies allowed.