RandomNumberFun
RandomNumberFun

Reputation: 642

having trouble with a "\n" and scanf

Here is the code

       printf("\n");
       printf("Enter a integer vaule:");
       scanf("%d" , &num3);
       printf("You entered: %015d", num3);


       printf("Enter a float value:");
       scanf("%f", &deci3);
       printf("You entered: %15.2f", deci3);
       printf("\n");

the output is

       Enter a integer vaule:4.4
       You entered: 000000000000004
       Enter a float value:You entered:            0.40

The problem is this code is not stopping at

       printf("Enter a float value:");

and this scanf

      scanf("%f", &deci3);

seems to be getting its value from the previous scanf

Upvotes: 2

Views: 319

Answers (4)

Alex_ban
Alex_ban

Reputation: 321

use a fflush(stdin) function after the fist scanf(), this will flush the input buffer.

Upvotes: -2

sbouba
sbouba

Reputation: 337

This works, but it dont complains if you type 4.4 for the int

#include <stdio.h>

int main() {

char buffer[256];
int i;
float f;

printf("enter an integer : ");
fgets(buffer,256,stdin);
sscanf(buffer, "%d", &i);
printf("you entered : %d\n", i);

printf("enter a float : ");
fgets(buffer,256,stdin);
sscanf(buffer, "%f", &f);
printf("you entered : %f\n", f) ;

return 0;
}

Upvotes: 0

impl
impl

Reputation: 803

The scanf function works this way per the specification:

An input item shall be defined as the longest sequence of input bytes (up to any specified maximum field width, which may be measured in characters or bytes dependent on the conversion specifier) which is an initial subsequence of a matching sequence. [Emphasis added.]

In your example, the following C string represents the contents of stdin when the first scanf call requests input: "4.4\n".

For this initial call, your format string consists of a single specifier, %d, which represents an integer. That means that the function will read as many bytes as possible from stdin which satisfy the definition of an integer. In your example, that's just 4, leaving stdin to contain ".4\n" (if this is confusing for you, you might want to check out what an integer is).

The second call to scanf does not request any additional input from the user because stdin already contains ".4\n" as shown above. Using the format string %f attempts to read a floating-point number from the current value of stdin. The number it reads is .4 (per the specification, scanf disregards whitespace like \n in most cases).

To fully answer your question, the problem is not that you're misusing scanf, but rather that there's a mismatch between what you're inputting and how you're expecting scanf to behave.

If you want to guarantee that people can't mess up the input like that, I would recommend using strtol and strtod in conjunction with fgets instead.

Upvotes: 0

Potatoswatter
Potatoswatter

Reputation: 137770

The %d conversion stops wherever the integer stops, which is a decimal point. If you want to discard the input there, do so explicitly… getc in a loop, fgets, or such. This also allows you to validate the input. The program should probably complain about 4.4.

Upvotes: 3

Related Questions