using scanf to gather an int and float?? 
Author Message
 using scanf to gather an int and float??

Been working at this for a couple of hours now but I
can't seem to figure out why my unit price keeps coming
up as garbage.

If you have a moment can you add some insight into this?

Thank You
Kevin

THE CODE:

int main(void)
{

     /* LOCAL DEFINITIONS */

int quantity;
float unit_price;

     /* STATEMENTS */
  printf("Please enter the quantity and the unit price for your items?\n");
  scanf("%d, %f", &quantity, &unit_price);

  printf("You entered a quantity of %d and a price of %8.2f\n\n", quantity,
unit_price);

return 0;

Quote:
} /* end main */



Sun, 29 Feb 2004 04:27:35 GMT  
 using scanf to gather an int and float??

Quote:

> Been working at this for a couple of hours now but I
> can't seem to figure out why my unit price keeps coming
> up as garbage.

> If you have a moment can you add some insight into this?

> Thank You
> Kevin

> THE CODE:

> int main(void)
> {

>      /* LOCAL DEFINITIONS */

> int quantity;
> float unit_price;

>      /* STATEMENTS */
>   printf("Please enter the quantity and the unit price for your items?\n");
>   scanf("%d, %f", &quantity, &unit_price);

You shouldn't use scanf in the first place as stated in the
faq. Use fgets and sscanf instead or even better fgets and
strtoXX.
Also always check for failure of functions that can fail. At
least add a if(scanf("%d, %f", &quantity, &unit_price)!=2){/*
handle error */} to your code.
cheers, Tobias.


Sun, 29 Feb 2004 05:08:13 GMT  
 using scanf to gather an int and float??
#include <stdio.h>
int             main(void)
{
    int             quantity;
    float           unit_price;
    int             count_converted;

    printf("Please enter the quantity and the unit price for your
items?\n");
    puts("scanf() is evil.  Read the C FAQ.");
    count_converted = scanf("%d %f", &quantity, &unit_price);
    if (count_converted == 2)
        printf("You entered a quantity of %d and a price of %8.2f\n\n",
               quantity,
               unit_price);
    else {
        puts("Well, that's scanf() for you!");
    }

    return 0;

Quote:
}

--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm


Sun, 29 Feb 2004 05:09:19 GMT  
 using scanf to gather an int and float??


Quote:
> #include <stdio.h>
> int             main(void)
> {
>     int             quantity;
>     float           unit_price;
>     int             count_converted;

>     printf("Please enter the quantity and the unit price for your
> items?\n");
>     puts("scanf() is evil.  Read the C FAQ.");
>     count_converted = scanf("%d %f", &quantity, &unit_price);
>     if (count_converted == 2)
>         printf("You entered a quantity of %d and a price of %8.2f\n\n",
>                quantity,
>                unit_price);
>     else {
>         puts("Well, that's scanf() for you!");
>     }

>     return 0;
> }
> --
> C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
>  "The C-FAQ Book" ISBN 0-201-84519-9
> C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm

I appreciate the enthusiasm here but this an introductory course to C
programming.

AS such I need to solve these problems using the tools at my disposal.
So far I have scanf, and printf to work with.

So if it is possible to explain to a newbie why the code failed and of
course
time permitting can you advise?

Thank You
Kevin

NOTE: I have read the advisory on gets, sscanf, and a few other items.
            Hopefully I will be able to work with those tools soon.



Sun, 29 Feb 2004 06:29:16 GMT  
 using scanf to gather an int and float??


Quote:
> Been working at this for a couple of hours now but I
> can't seem to figure out why my unit price keeps coming
> up as garbage.

> If you have a moment can you add some insight into this?

> Thank You
> Kevin

> THE CODE:

> int main(void)
> {

>      /* LOCAL DEFINITIONS */

> int quantity;
> float unit_price;

>      /* STATEMENTS */
>   printf("Please enter the quantity and the unit price for your
items?\n");
>   scanf("%d, %f", &quantity, &unit_price);

>   printf("You entered a quantity of %d and a price of %8.2f\n\n",
quantity,
> unit_price);

> return 0;
> } /* end main */

Hi,

I'd like to echo Tobias' comment and say don't use scanf because it has
a few traps for the unwary. I'm also assuming that you remembered to
#include <stdio.h>.

However to enter the data for your program above, you must satisfy the
format specifier you have given scanf or it will not complete the
conversion of the values.
The format you have given is "%d, %f". This means an int, followed by a
comma, followed by zero or more white space characters, followed by a
floating point value (although a value without a decimal point should be
acceptable).
I suspect that you have entered values without including the comma
separator, and scanf is only able to convert the first integer.
Note that even if you enter the values correctly there will still be a
line feed character left on the input stream, corresponding to Return
key.

    Regards
    Des Walker



Sun, 29 Feb 2004 06:36:45 GMT  
 using scanf to gather an int and float??

[snip]

Quote:
>I appreciate the enthusiasm here but this an introductory course to C
>programming.

>AS such I need to solve these problems using the tools at my disposal.
>So far I have scanf, and printf to work with.

>So if it is possible to explain to a newbie why the code failed and of
>course
>time permitting can you advise?

When building the code that you posted I got the following warning:

  ld32: WARNING 110: Floating-point parameters exist in the call for
  "printf", a VARARG function, in object "foo.o" without a prototype --
  would result in invalid result.  Definition can be found in object
  "/usr/lib32/mips3/libc.so"

If I include stdio.h at the top of the file then the warning (and the
problem) goes away.

Next time, turn on all the warning flags in your compiler (read the
docs to discover how to do this.  It's different for different systems).
Then, eliminate all warnings from your code before running it.

Alan



Sun, 29 Feb 2004 06:49:46 GMT  
 using scanf to gather an int and float??

Quote:



> > Been working at this for a couple of hours now but I
> > can't seem to figure out why my unit price keeps coming
> > up as garbage.

> > If you have a moment can you add some insight into this?

> > Thank You
> > Kevin

> > THE CODE:

> > int main(void)
> > {

> >      /* LOCAL DEFINITIONS */

> > int quantity;
> > float unit_price;

> >      /* STATEMENTS */
> >   printf("Please enter the quantity and the unit price for your
> items?\n");
> >   scanf("%d, %f", &quantity, &unit_price);

> >   printf("You entered a quantity of %d and a price of %8.2f\n\n",
> quantity,
> > unit_price);

> > return 0;
> > } /* end main */

> Hi,

> I'd like to echo Tobias' comment and say don't use scanf because it has
> a few traps for the unwary. I'm also assuming that you remembered to
> #include <stdio.h>.

> However to enter the data for your program above, you must satisfy the
> format specifier you have given scanf or it will not complete the
> conversion of the values.
> The format you have given is "%d, %f". This means an int, followed by a
> comma, followed by zero or more white space characters, followed by a
> floating point value (although a value without a decimal point should be
> acceptable).
> I suspect that you have entered values without including the comma
> separator, and scanf is only able to convert the first integer.
> Note that even if you enter the values correctly there will still be a
> line feed character left on the input stream, corresponding to Return
> key.

>     Regards
>     Des Walker

I completely missed the comma between the scanf values.

I also here what your saying about scanf, and I plan to follow
the advise as soon as possible. But because this is an intro
course I would like to stay with the program until I get more
comfortable with the material.

If I remember correctly the functions you speak of won't be
introduced till about the 5th or 6th chapter so I will have to wait
abit.

Thank You
Kevin



Sun, 29 Feb 2004 06:58:58 GMT  
 using scanf to gather an int and float??

[snip]

Quote:
> >     count_converted = scanf("%d %f", &quantity, &unit_price);
[snip]

> I appreciate the enthusiasm here but this an introductory course to C
> programming.

> AS such I need to solve these problems using the tools at my disposal.
> So far I have scanf, and printf to work with.

> So if it is possible to explain to a newbie why the code failed and of
> course
> time permitting can you advise?

> Thank You
> Kevin

> NOTE: I have read the advisory on gets, sscanf, and a few other items.
>             Hopefully I will be able to work with those tools soon.

Look carefully at the format specifier you supplied and the one that I
supplied to scanf().

You should always check the return values of library functions, and for
scanf() it should be absolutely mandatory.  That will be your first clue
that something went wrong.  Then the surprising return values will cause you
to investigate your C manual.
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm



Sun, 29 Feb 2004 07:05:59 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. crash when using scanf() to assign int value to struct member

2. scanf bug when retrieving a float value from a pointer to a float, in a structure

3. float = int / int; Hdw do I do this

4. Danger in using int and unsigned int together?

5. error checking in scanf: expecting int but user enter char

6. reading int's without *scanf

7. Help: scanf problem with short/int

8. scanf : floating point format not linked

9. scanf problem with float

10. int func(int) versus int func(int *) efficacy.

11. Help with scanf and double float input

12. Error !!! scanf : floating point formats not linked

 

 
Powered by phpBB® Forum Software