?
Solved

Using scanf on front end Graphics page

Posted on 2003-03-19
10
Medium Priority
?
479 Views
Last Modified: 2010-04-15
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
Comment
Question by:ms33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 8167442
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 8172484
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8173261
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Mayank S
ID: 8173298
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
 

Author Comment

by:ms33
ID: 8173329
Thanks for the input guys
But exactly how do I go about making a graphic I/O??
Regards
Ms
0
 
LVL 16

Expert Comment

by:imladris
ID: 8175773
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8178837
>> 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
 

Author Comment

by:ms33
ID: 8179534
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8179558
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
 
LVL 16

Expert Comment

by:imladris
ID: 8196156
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question