hi!

i want to move a character A on the screen by using arrow keys.
i know that there are 25 rows and 80 columns.i take a character input as follows:

char input;
cin>>input;

int put=input;

here, the ascii value of input goes in put.
i am using the ascii codes of arrows from the first table given in the link:

but when i apply if condition that:

if(put==29)
{
    gotorc(i,j+1);
    putchar('A');
}

//j+1 means that the character should move one column forward

the character stays still.
it doesn't move a column even

please help me

Dani AI

Generated

The problem is the input method and expectation about arrow keys. Reading with formatted input (for example cin >> ...) waits for Enter and only returns printable characters; arrow keys are handled by the terminal as special key sequences, not single printable ASCII values. Also make sure you really are building a console program with Visual C++ (VB6 cannot compile C++ — as pointed out) and confirm which compiler/version you are using (as asked).

On Windows (MSVC/VC++) a quick approach is to use _getch() from <conio.h> and the Win32 console API to move the cursor. Arrow keys come through as a two-step sequence: the first _getch() returns 0 or 224, the second returns the actual code (72 = up, 80 = down, 75 = left, 77 = right). Example (minimal, for a console project):

#include <windows.h>
#include <conio.h>
#include <iostream>

int main() {
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos = {40, 12};      // X = column, Y = row
    char glyph = 'A';
    SetConsoleCursorPosition(h, pos);
    std::cout << glyph;

    while (true) {
        int k = _getch();
        if (k == 0 || k == 224) {
            int code = _getch();
            int dx = 0, dy = 0;
            if (code == 72) dy = -1;   // up
            else if (code == 80) dy = 1; // down
            else if (code == 75) dx = -1; // left
            else if (code == 77) dx = 1;  // right

            SetConsoleCursorPosition(h, pos);
            std::cout << ' ';                     // erase old
            short newX = pos.X + dx;
            short newY = pos.Y + dy;
            if (newX < 0) newX = 0; if (newX > 79) newX = 79;
            if (newY < 0) newY = 0; if (newY > 24) newY = 24;
            pos.X = newX; pos.Y = newY;
            SetConsoleCursorPosition(h, pos);
            std::cout << glyph;
        } else if (k == 27) break; // ESC to quit
    }
    return 0;
}

Notes and troubleshooting:

  • If you see nothing happen, confirm you are running a console app (not a GUI project) and that _getch() is available.
  • If you target Unix-like systems, arrow keys send escape sequences (27, '[', 'A'/'B'/'C'/'D'); use a library like ncurses there.
  • Clear the previous character (print a space) before drawing at the new position to avoid leaving a trail.
  • If you used a custom function like gotorc, replace it with the Win32 call above or a known console helper — gotorc is not a standard MSVC function.

Recommended Answers

All 10 Replies

On which compiler are u using?

i m using visual basic six professional edition

If you are using VB6 then you are using the wrong compiler. VB6 can not compile c or c++ code.

no sir, it does.
i have been using VB6 since last 2 years. and m studying c++

You may have been using vb6 but I will guarentee on my deathbed that you are not compiling c++ code with it. Maybe you mean VC++ 6.0??? Those two are not the same.

yes, it's visual c++

The best C++ compiler is Borland Turbo C++. It provides you great programming environment.

commented: Completely irrelevant and opinionated reply -2

The best C++ compiler is Borland Turbo C++. .

Only if you are 10 years old or younger. No adult would think of using that compiler any more for anything other than a toy to play with.

but i am just a beginner in C++.. thats why i use it.

but i am just a beginner in C++.. thats why i use it.

Use a modern compiler such as Code::Blocks so that you can learn how to write c++ programs correctly. Would you try to learn how to repair 2011 model automobils by using a 1920 Ford? Of course not! So why use a 1980 compiler to write 2011 programs on 2011 operating systems?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.