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;
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:
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'
#include <iostream>
usingnamespace 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?
> 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.