Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inputting float into a program that only deals with ints

I have a program, but when I input float numbers whenever the program asks for inputs, the program abruptly skips a step and moves onto the end output. The program is below:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int a,b,c;
  int i;

  printf("Please enter a number: ");
  scanf("%d", &a);
  printf("Please enter a number: ");
  scanf("%d", &b);

  c = 0; 
  for(i=0; i < b; i++)
    {
    c = c + a;
    } 

  printf("%d x %d = %d\n", a, b, c);

  return 0;
}

When I input an int for a, and a float for b, the program will output the product as expected if the numbers after the decimal point for b is truncated. However when I input a float for a, the program doesn't take the value for the second number b and instead skips that step and outputs the integer version of a x -858993460 = 0.

For example:

a = int, b = float

Please enter a number: 3
Please enter a number: 5.6
3 x 5 = 15

a = float, b = skipped

Please enter a number 3.9
Please enter a number: 3 x -858993460 = 0

All the flaws in the code are deliberate, but I just wanted to know why it behaves the way I explained above. I know it's because of something to do with trying to input a float into a signed integer but I'm not sure what exactly is causing it to skip the second scanf("%d", &b). Can anyone explain why this happens?

Thanks.

like image 314
Jigglypuff Avatar asked Aug 03 '11 06:08

Jigglypuff


People also ask

Can you use float for integers?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function.

Can I store float in int or not with explanation?

So you cannot store a float value in an int object through simple assignment. You can store the bit pattern for a floating-point value in an int object if the int is at least as wide as the float , either by using memcpy or some kind of casting gymnastics (as other answers have shown).

What happens when you assign a float to an int?

You can safely assign a floating point variable to an integer variable, the compiler will just truncate (not round) the value. At most the compiler might give you a warning, especially if assigning from e.g. a double or bigger type.

Can I use %d for float?

So, you can see here that %d is used for integers, %f for floats and %c for characters. As simple as that!


1 Answers

It looks like scanf() is reading your "3" in the second case, and ignoring the "9".

Then when the second scanf() is called, there is already text in the input buffer (the ".9").

I can't tell exactly what it's doing with the ".9". It may have found the dot and just aborted there with b uninitialized. It should be a simple matter to determine what is happening by stepping through with the debugger.

But, basically, not all the input is being processed by the first call to scanf() and so that's what the second call is trying to read. And that's why it's not waiting for you to input any data for the second call.

like image 171
Jonathan Wood Avatar answered Sep 25 '22 20:09

Jonathan Wood