Newbie problem w/ scanf
Author Message
Newbie problem w/ scanf

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() {

double a, b, c;

printf("Enter the length of the first side here: ");
scanf("&lf", &a);
printf("Enter the length of the second side here: ");
scanf("&lf", &b);
c = sqrt(a * a + b * b);
printf("The hypotenuse is %lf\n", c);

Quote:
}

Thanks,

Wayne

Thu, 18 Jul 2002 03:00:00 GMT
Newbie problem w/ scanf

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() {

>         double a, b, c;

>         printf("Enter the length of the first side here: ");
>         scanf("&lf", &a);
>         printf("Enter the length of the second side here: ");
>         scanf("&lf", &b);
>         c = sqrt(a * a + b * b);
>         printf("The hypotenuse is %lf\n", c);
> }

GCC and LCLint say:

scnf.c:4: warning: return-type defaults to `int'
scnf.c: In function `main':
scnf.c:9: warning: too many arguments for format
scnf.c:11: warning: too many arguments for format
scnf.c:13: warning: use of `l' length character with `f' type character
scnf.c:14: warning: control reaches end of non-void function

LCLint 2.4b --- 18 Apr 98

scnf.c: (in function main)
scnf.c:9:9: Format string for scanf has 0 args, given 1
Types are incompatible. (-type will suppress message)
scnf.c:9:9: Return value (type int) ignored: scanf("&lf", &a)
Result returned by function call is not used. If this is intended, can cast
result to (void) to eliminate message. (-retvalint will suppress message)
scnf.c:11:9: Format string for scanf has 0 args, given 1
scnf.c:11:9: Return value (type int) ignored: scanf("&lf", &b)
scnf.c:12:18: Variable a used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (-usedef will suppress message)
scnf.c:12:26: Variable b used before definition
scnf.c:14:2: Path with no return in function declared to return int
There is a path through a function declared to return a value on which there
is no return statement. This means the execution may fall through without
returning a meaningful result to the caller. (-noret will suppress message)

Finished LCLint checking --- 7 code errors found

Chances are your compiler will tell you similar things if you use it at
the highest warning level as you should. You should also read your
book's description of scanf() again and see the FAQ for why it's not a
good function to use: http://www.eskimo.com/~scs/C-faq/top.html

Gergo

--
Heller's Law:
The first myth of management is that it exists.

Johnson's Corollary:
Nobody really knows what is going on anywhere within the
organization.

GU d- s:+ a--- C++>\$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+

Thu, 18 Jul 2002 03:00:00 GMT
Newbie problem w/ scanf
Wayne Willson a crit dans le message ...

Quote:
>I'm trying to use scanf to get input from the user.  Why doesn't the second
>scanf wait for user input?

Simply because scanf() is tricky. Use fgets(,,stdin) and strtod() instead.
Note aslo that you must use fflush(stdout) after the unterminated lines
given to printf().

Quote:
>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() {

>        double a, b, c;

>        printf("Enter the length of the first side here: ");

fflush(stdout);

Quote:
>        scanf("&lf", &a);

A very basic approach :
{
char s[40];

fgets(s, sizeof s, stdin);
a=strtod(s);

Quote:
}
>        printf("Enter the length of the second side here: ");
>        scanf("&lf", &b);
>        c = sqrt(a * a + b * b);
>        printf("The hypotenuse is %lf\n", c);

AFAIK, the printf() format for both float and double is "%f"

Main() returns an int. You must add a return here :

return 0;

Quote:
>}

--
-hs-
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC

Thu, 18 Jul 2002 03:00:00 GMT
Newbie problem w/ scanf
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:
> }

> Thanks,

> Wayne

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

Thu, 18 Jul 2002 03:00:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages