[comp.sys.mac.programmer] MPW C 3.1 scanf problem

David_Anthony_Guevara@cup.portal.com (01/22/91)

I have a (hopefully) real simple problem.  I am using MPW C 3.1
to do some programming for a C class.  What I want to do is to prompt
the user and leave the cursor at the end of the prompt line.  My problem
is that scanf will not read the input if the cursor is at the end of the
prompt line.  I have to start on a newline for scanf to get the value.
Example code:

#include <stdio.h>
main()
{
     int input1;

     printf("Input an integer value: ");  /* cursor is at end of line */
     scanf("%d",&input1);
     printf("input1 = %d\n",input1);
}

If I put a newline in after the prompt, the scanf works.  If I leave the
code as stated above, I'll get some garbage.  I'm building this under
MPW as a tool.  Is this a known problem/undocumented feature of MPW C?
I've compiled the same code under Think C 4.02 and Turbo C with no problem.
E-mail me your responses.  I'm sure this is too elementary to waste on the
net.  As always, I will summarize to the net if there is sufficient interest.

	Dave Guevara,
	Internet:  David_Anthony_Guevara@cup.portal.com

jhgillespie@ucdavis.edu (01/23/91)

In article <38337@cup.portal.com> David_Anthony_Guevara@cup.portal.com writes:
>What I want to do is to prompt
>the user and leave the cursor at the end of the prompt line.  My problem
>is that scanf will not read the input if the cursor is at the end of the
>prompt line.  I have to start on a newline for scanf to get the value.

try calling fflush(stdout) after the printf()

John Gillespie
jhgillespie@ucdavis.edu

ml27192@uxa.cso.uiuc.edu (lanett mark) (01/23/91)

David_Anthony_Guevara@cup.portal.com writes:

>I have a (hopefully) real simple problem.  I am using MPW C 3.1
>to do some programming for a C class.  What I want to do is to prompt
>the user and leave the cursor at the end of the prompt line.  My problem
>is that scanf will not read the input if the cursor is at the end of the
>prompt line.  I have to start on a newline for scanf to get the value.
[example code deleted]

MPW Buffers lines, and a scanf or anything will read in _everything_ on the
cusor's line. You must do the newline before.

Mark Lanett

Jim.Spencer@p510.f22.n282.z1.fidonet.org (Jim Spencer) (01/24/91)

In a message <38337@cup.portal.com> 22 Jan 91 David_Anthony_Guevara@cup.portal.com writes
D> If I put a newline in after the prompt, the scanf works. If I 
D> leave the code as stated above, I'll get some garbage. I'm building 
D> this under MPW as a tool. Is this a known problem/undocumented 
D> feature of MPW C?

What you are seeing is MPW's normal way of passing input: just as when you send a command, if you have a selection, the selection will be passed.  If you have no selection but simply have your cursor some place in the line, then the whole line is getting sent.  

When you do textbook type code with things like scanf(), you will have the cursor at the end of your prompt, you then type the integer in your case and press enter.  What gets sent is the entire line, INCLUDING THE PROMPT.  You aren't really getting garbage: you are seeing the result of scanf() trying to interpret "Input" as an integer.  Your options are to either put a carriage return in or to select the integer before pressing the enter key.