#### Archived

This topic is now archived and is closed to further replies.

# Position in a C++ Console App

## Recommended Posts

I want to make some text display in center of the screen in a C++ console app. I am using MVC++ and I tried using SetConsoleCursorPosition() and including windows.h, but it doesnt work and I dont know how to use it exactly. Thanks alot.

##### Share on other sites
if you include iomanip.h, you can use a function called setw(), which allows you to set blank width since the last printed character, right justified.

cout << "Blah" << setw(5) << "Blah";

of course, that would only put the blahs 1 space from eachother, because from the right side of the second blah to the end of the other blah is 5, leaving one space, but its good for setting tables allignment and what not.

also you can use endl, lkike this:

cout << "This is a line of code" << endl;

that would end the line, so when you cout next, it will be on the next line. you create your own position setter, you could make a function like this:

void SetConsoleCursorPos(int x, int y)
{
//Sets y position first
for(int i = 0; i < y; i++)
cout << endl;

//Sets x position
cout << setw(x);
}

that would put the next printed character where you wanted it

##### Share on other sites
Ok, I got the cursor position to work, but I have googled this for about an hour now looking for this. I want to move the cursor by pressing the left, right, up and down arrow keys but I cant figure out how to tell if they are pressed or not. I was going to do something like:

if left key is pressed, subtract one from the value of the x coordinate. If up key is pressed add one to the value of y and so on and then update the screen. I dont even know if it would update the screen in realtime. I am just making an attempt at making something move using the keyboard. Any more help would be apreciated.

##### Share on other sites
You can use getch() in a loop to continuously get input from the user. Remember to include conio.h

--{You fight like a dairy farmer!}

##### Share on other sites
but how do i know which key they are pressing? Isnt getch() just for any key?

##### Share on other sites
edit: Sorry about that, im so tired that my answer barely made sense so i took it out.

*yawns*

[edited by - Tiffany_Smith on July 28, 2003 1:44:53 AM]

##### Share on other sites
getch() will return an int that will contains the ASCII value of the key pressed.

MSDN says that you need to call getch() twice for the function or arrow keys, the first call will return 0 or 0x0E and the second call will return the actual key code of the key pressed so you might want to bear that in mind.

##### Share on other sites
quote:
Original post by NashuNatta
I want to make some text display in center of the screen in a C++ console app. I am using MVC++ and I tried using SetConsoleCursorPosition() and including windows.h, but it doesnt work and I dont know how to use it exactly. Thanks alot.
The solution someone else wrote sounds like it would work, but the way I get around the lack of a Location function (ala qBasic) is to create an array the size of my screen, edit that array as desired, then post it to the screen. Functions to look at would be WriteConsoleOutput() and SetActiveScreenBuffer() (I think that''s the name of it)

If you remember the ASCII competition in the Lounge, I''d be willing to bet that 4/5 entries (excluding mine) used the above method.

##### Share on other sites
conio.h isn''t part of the standard, by the way. As far as I know, you should only be able to #include <conio.h> and use
getch() if you''re using msvc++. Other compilers probably have equivalents.

There is documentation somewhere for what getch() returns for the non-ACII keyboard buttons. For all the rest, it returns the normal ACSII values (duh ).

##### Share on other sites
Getch() does not return the ASCII value of a key. It randomly spits out shit. I looked at an ascii chart [url=http://www.jimprice.com/ascii-0-127.gif]here[/url] and none of that matchs what gets spit out on my screen. When I press the down key with two getch(), sometimes I get 70 sometimes I get 240. Acourdig to the chart the numeral value is 031. Can someone please help?

##### Share on other sites
heres a little snippet from a game ive been working on, Im unclear as to what youre trying to do but maybe this will help ...
 		switch(_getch())			{				case 75:					bLeft = true;					//left					break;				case 77:					bRight = true;					//right					break;				case 72:					bRotCounterClock = true;					//up					break;				case 80:					bRotClock = true;					//down					break;			}

As you can see, for the arrow keycodes i have 75 for left, 77 for right, 72 for up and 80 for down.

Is this what you are looking for?

##### Share on other sites
wow. That was the stupidest problem ever. I didnt add the _ before getch(). I dont know why _getch() is diff from getch() but it is and that is what made the diff. Thanks alot.

##### Share on other sites
The underscore indicates that the function is non-standard (of course, just because a function doesn''t have one, doesn''t mean that it''s standard). Whenever you can use a standard function, do. The standard equivalent to getch is getchar, which is in stdio.h.

##### Share on other sites
quote:
Original post by NashuNatta
wow. That was the stupidest problem ever. I didnt add the _ before getch(). I dont know why _getch() is diff from getch() but it is and that is what made the diff. Thanks alot.

_getch() is the actual library function, getch() is just a #define to it.

youre welcome.

##### Share on other sites
quote:
I said ...
_getch() is the actual library function, getch() is just a #define to it.

Hrmmm it appears I could be wrong about that, hopefully someone with a little more experience than myself will come along and set the record straight, because it would be interesting to know.

• ### Forum Statistics

• Total Topics
628307
• Total Posts
2981972

• 9
• 13
• 11
• 12
• 11