[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

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
0
ms33
Asked:
ms33
  • 4
  • 3
  • 2
  • +1
1 Solution
 
imladrisCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I think that you can use the window () function. It creates a text-window and prints text there. You can also read text from there.

I don't know what your resolution is, but I'm giving you a simple example:

char str[80] ;
window ( 10, 10, 60, 5 ) ;
printf ( " Enter data: " ) ;
scanf ( "%s", str ) ;

See if it helps you!

Mayank.
0
 
KocilCommented:
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 ?

0
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
ms33Author Commented:
Thanks for the input guys
But exactly how do I go about making a graphic I/O??
Regards
Ms
0
 
imladrisCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
ms33Author Commented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
imladrisCommented:
Assuming that we have helped you on your way with your question, it is now time to pick an answer and grade it.
0

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now