Link to home
Start Free TrialLog in
Avatar of ms33
ms33

asked on

Using scanf on front end Graphics page

Have written a program in dos BC5. Designed a front end graphic screen and on that screen want to use scanf to allow user to input some data. However when I use scanf the program waits for the input on the top left corner of the screen as apposed to the center where i require it to be. How can I locate the printf statement and scanf response in middle of my graphics screen...What am I doing wrong
Thanks
Ms
Avatar of imladris
imladris
Flag of Canada image

printf and scanf were an original part of the C compiler, which was written in the days of commandline interfaces. Graphic interfaces were rare for another decade or more after that. Even DOS itself is still largely a commandline environment.

So, to do I/O in a graphics environment you should really use different means. Assuming you have the documentation for your Borland compiler, you should be able to find a command to position the cursor (something like gotoxy(x,y)). Then you can show characters on the screen at that position, which should probably be done with console I/O functions. Borland probably comes with something like cprintf. To get input you should probably use something like getch or getche; character at a time console input routines. You will then have to assemble these into strings, and parse them yourself.
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kocil
Kocil

If you use scanf or scanf, thats mean you use text mode I/O. No matter what your graphic resolution is, the text will always 25 row x 80 column or 25 rows x 40 column.

To get the input from the centre of the screen, you may use

gotoxy(1,15);
scanf("%s", str);

However, I have to say that using text based I/O on a graphics screen is a bad idea. Why don't you make your own graphic I/O ?

Dear Kocil,

I agree with you but I guess that gotoxy () might not work on graphics mode for scanf (). The window () works, at least in DOS. And yeah, it'll be for the 25 X 80 model (no matter what the screen resolution is). That's why I gave 10, 60, etc for the window limits. In fact, the last argument is the bottom of the window so I made a mistake in putting 5 - should've put 15! I agree that its a bad idea to use scanf () in graphics.

Personally, I'd advise him to switch over to Java :-)

Mayank.
Avatar of ms33

ASKER

Thanks for the input guys
But exactly how do I go about making a graphic I/O??
Regards
Ms
Assuming that you are operating in the confines suggested (80x25 text window) you can do it along the lines I suggested.

Use gotoxy to position the cursor, then use cprintf to display any text. To do input, use getch or getche to assemble the input one character at a time, and then parse it yourself. For instance, if you need a number, get each character, ensure they are all digits, and at the end use atoi to convert the string to an integer.
>> Have written a program in dos BC5. Designed a front end graphic screen

>> But exactly how do I go about making a graphic I/O??

These statements are very contradictory. Please specify what exactly do you want to know about graphics. I hope that you are cognizant with initgraph (), colours, fonts, etc.

Mayank.
Avatar of ms33

ASKER

In response to Mayank's statement...I'm currently using gotoxy() , cprintf and scanf statements to retrive the data...But one of the suggestions above said I should not use the scanf approach. This confused me a little as I thought I was using a grapic I/O approach and thats why I asked the question regarding how to make graphic I/O!!!

What I'll try instead is to replace the scanf with the getch approach. Although I admit to not being an expert...this is my first dos based grapic I/O(usually work in visual c...but had no choice with this one)Am a little cognizant with initgraph etc. It's just that because I always working in visiual envirment.... front end screens are simply drag and place etc and so get taken for granted.
 Thanks guys for your patience on this one as you have been a big help
Regards
Ms
All right. If you're using cprintf () and scanf () only, then your can safely proceed with the window () approach. You don't need to replace scanf () with getch (). That'll be very cumbersome while reading strings and numbers. I was initially under the impression that you were working under the graphics mode initialized by initgraph () but it looks as though you're working under the normal 25 X 80 text-mode and using the functions in <conio.h> for some coloured displays, etc. Go ahead.... try the window () function.

Mayank.
Assuming that we have helped you on your way with your question, it is now time to pick an answer and grade it.