Hey All!

kindly please tell me how to get user input right after the outtextxy function in graphics mode. Compiler : Turbo C , Win XP

here is my program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd=detect,gm,s;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
outtextxy(100,120,"enter number here : ");
scanf("%d",&s);
getch();
}

now here what i want is the cursor to be displayed right after where "Enter number here" gets printed and takes the user input there.. but scanf is getting the input on top left corner.. kindly tell me if there is any replacement for scanf in graphics mode or anything else... thanks

Dani AI

Generated

Short answer: outtextxy writes to the graphics (BGI) screen while scanf reads from the text console. That is why your input appears at the top-left console cursor instead of next to the graphics text. As pointed out, gotoxy() moves the text-mode cursor — it will not place a cursor inside the graphics window. The usual solutions are: (A) do the console input (scanf/printf/gotoxy) before you switch to graphics, or (B) implement input entirely in graphics mode by reading keystrokes (getch/kbhit) and echoing characters with outtextxy.

Below is a compact, practical graphics-mode input routine you can drop into a Turbo C / BGI program. It captures characters, handles Backspace and Enter, and redraws the string at the given (x,y). Use atoi() after return to convert to an integer.

/* simple graphics input: echoes chars at (x,y), returns on Enter */
void gfx_input(int x, int y, char *buf, int maxlen)
{
    int pos = 0;
    int ch;
    buf[0] = '\0';
    while (1) {
        ch = getch();
        if ((unsigned)ch == 0) { getch(); continue; } /* extended key */
        if (ch == 13) break;           /* Enter */
        if (ch == 8) {                 /* Backspace */
            if (pos > 0) {
                pos--; buf[pos] = '\0';
                setfillstyle(SOLID_FILL, getbkcolor());
                bar(x, y - textheight("M"), x + textwidth("M") * maxlen, y + textheight("M"));
                outtextxy(x, y, buf);
            }
            continue;
        }
        if (ch >= 32 && ch <= 126 && pos < maxlen - 1) {
            buf[pos++] = (char)ch; buf[pos] = '\0';
            setfillstyle(SOLID_FILL, getbkcolor());
            bar(x, y - textheight("M"), x + textwidth("M") * maxlen, y + textheight("M"));
            outtextxy(x, y, buf);
        }
    }
}

Usage outline:

  • Draw the prompt with outtextxy.
  • Compute input X as promptX + textwidth(prompt).
  • Call gfx_input(inputX, promptY, buf, sizeof buf).
  • Convert with atoi(buf) or strtol.

Tips: use textwidth/textheight for correct placement, redraw the input area (bar) before each repaint, and optionally draw a blinking caret by toggling an underscore at x + textwidth(buf). If you must use scanf, do it before initgraph() or call closegraph() so the console is visible. Turbo C/BIg is old—on modern systems consider using a contemporary library (SDL, Win32 GUI, or a modern compiler) for graphics work.

Recommended Answers

All 2 Replies

Move the cursor with gotoxy() . Just because you output text at a certain location doesn't mean the cursor was moved.

up

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.