can anyone tell me why?

i got a program in which there are two statements

scanf( "%7.2f", &len );
scanf( "%7.2f", &wid );

(note: len and wid has been declared as float and initialized to 0 before, no problem with anything else)

but scanf can not scan the input real number, the following statement

printf( "%7.2f", area = len * wid );

always show me a 0. And i modified the scanf statements to
i = scanf( "%7.2f", &len );
j = scanf( "%7.2f", &wid );
and print i, j using a printf, to my surprise, they are both 0, which means the scanf failed to scan


Can anyone tell me why? Thanks!

Comments

  • : i got a program in which there are two statements
    :
    : scanf( "%7.2f", &len );
    : scanf( "%7.2f", &wid );
    :
    : (note: len and wid has been declared as float and initialized to 0 before, no problem with anything else)
    :
    : but scanf can not scan the input real number, the following statement
    :
    : printf( "%7.2f", area = len * wid );
    :
    : always show me a 0. And i modified the scanf statements to
    : i = scanf( "%7.2f", &len );
    : j = scanf( "%7.2f", &wid );
    : and print i, j using a printf, to my surprise, they are both 0, which means the scanf failed to scan
    :
    :
    : Can anyone tell me why? Thanks!
    :
    [blue]
    Just in case you havnt found the answer yet...

    In the format specifier [b]%xf[/b], x specifies the number of characters to read into the buffer. x can only be a whole number.

    In the expression [b]scanf( "%7.2f", &wid );[/b], 7.2 is interruperated as "read in 7.2 characters"--which scanf does not accept. (There is no such thing as 7.2 characters, so scanf only accepts interegers)
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion