Board index » delphi » problem with scanf in cpp builder 1.0

problem with scanf in cpp builder 1.0

I am somewhat of a beginning programmer when it comes to c/c++ and I am having difficulties getting the scanf function to work in my c programs in c++ builder 1.0.  The following is sample code from a simple program I wrote to test the scanf function:
#include <stdio.h>

int main(void)
{

int x;

printf("enter value for x");
scanf("%d", &x);
printf("x is %d", x);

getchar();
return 0;

Quote
}

When I compile and run this code, the outcome is that the console will pop up, display the first printf, wait on my input for the scanf, and then close as soon as I enter a value and hit enter on my keyboard.  When I step through the program code by hitting F8, the program runs just fine!  Does anyone have a solution for this?  I'm running C++ Builder 1.0 on Windows2000 SP2, PIII 800MHz, 128MB PC133 SDRAM.  If any other information is needed to diagnose the problem, please let me know.

Thanks in advance,
newbie

 

Re:problem with scanf in cpp builder 1.0


On 24 Apr 2002 19:40:47 -0700, "newb2k2" <flyermax...@yahoo.com> wrote:

Quote
>I am having difficulties getting the scanf function to work in
>my c programs in c++ builder 1.0.  

Wrong newsgroup. This one is about the pre-Builder compilers up to
BC++ 5.02. Post Builder questions in one of the .cppbuilder. groups.
e.g. - borland.public.cppbuilder.language

Quote
>...wait on my input for the scanf, and then close as soon as I enter a value and hit enter

The getchar() is getting the residual newline char from the input stream
which came from the "Enter" key. To get a pause, either:

(1) add a second getchar().
or
(2) use the assignment suppression character in the scanf():
e.g. - scanf("%d%*c", &x);

--
Wayne A. King
(ba...@torfree.net, wayne.k...@ablelink.org,
 wak...@idirect.com, Wayne_A_K...@compuserve.com)

Other Threads