On Sun, 30 Jan 2000 20:37:43 GMT, "Wayne Willson"
Quote:
> I'm trying to use scanf to get input from the user. Why doesn't the second
> scanf wait for user input?
> The output is:
> Enter the length of the first side here: 8
> Enter the length of the second side here: The hypotenuse is 0.000000
> The source is:
> #include <stdio.h>
> #include <math.h>
> main() {
Make this:
int main(void) {
because implicit int is now illegal under the latest version of the C
standard.
Quote:
> double a, b, c;
> printf("Enter the length of the first side here: ");
> scanf("&lf", &a);
scanf("%lf", &a);
Quote:
> printf("Enter the length of the second side here: ");
> scanf("&lf", &b);
scanf("%lf", &b);
Quote:
> c = sqrt(a * a + b * b);
> printf("The hypotenuse is %lf\n", c);
printf("The hypotenuse is %f\n", c);
Quote:
Unless you already have a C99 conforming compiler, in which case it
should object to leaving the int off the definition of main(), there
is no "%lf" conversion specifier to printf(). "%f" is used for both
float and double arguments, because floats are explicitly promoted to
doubles when passed to variadic functions like printf().
And of course the scanf() conversion specifiers start with "%", not
"&", just like the printf() conversion specifiers do.
Next try entering "xyz" into one of the prompts for a number and watch
what happens. scanf() is a poor function for user input, but it does
return a value indicating success of failure and you are not checking
it.
See the FAQ for this group (link in my sig) and read up about all of
the other problems with scanf().
Jack Klein
--
Home: http://jackklein.home.att.net
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.cerfnet.com/~mpcline/c++-faq-lite
alt.comp.lang.learn.c-c++
http://www.raos.demon.co.uk/acllc-c++/faq.html