Position in a C++ Console App

Started by
13 comments, last by NashuNatta 20 years, 8 months ago
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.
____________________________________________________________[email=UntimelyDeathProductions@yahoo.com][E-mail me][/email]
Advertisement
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
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
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.
____________________________________________________________[email=UntimelyDeathProductions@yahoo.com][E-mail me][/email]
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!}

--{You fight like a dairy farmer!}

but how do i know which key they are pressing? Isnt getch() just for any key?
____________________________________________________________[email=UntimelyDeathProductions@yahoo.com][E-mail me][/email]
edit: Sorry about that, im so tired that my answer barely made sense so i took it out.

*yawns*

*heads off to bed*

[edited by - Tiffany_Smith on July 28, 2003 1:44:53 AM]
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.
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.
Peon
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 ).
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
Getch() does not return the ASCII value of a key. It randomly spits out shit. I looked at an ascii chart here 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?
____________________________________________________________[email=UntimelyDeathProductions@yahoo.com][E-mail me][/email]

This topic is closed to new replies.

Advertisement