1

I have a very simple program that simply asks for ones weight and converts it to the value of platinum. I am new to C so the mistake could be anywhere. But when I use scanf, it asks for input at the very beginning rather than following the sequence of code:

code:

 #include <stdio.h>
 int main(void)
 {
     float weight;
     float value;

     printf("Are you worth your weight in platinum?\n");
     printf("Let's check it out.\n");
     printf("Please enter your weight in pounds: ");
     scanf("%f", &weight);
     printf("%.2f\n", weight);
     value = 1700.0 * weight * 14.5833;
     printf("Your weight in platinum is worth $%.2f.\n", value);
     printf("You are easily worth that! If platinum prices drop,\n");
     printf("eat more to maintain your value.\n");

     return 0;

 }

Output:

123
Are you worth your weight in platinum?
Let's check it out.
Please enter your weight in pounds: 123.00
Your weight in platinum is worth $3049368.00.
You are easily worth that! If platinum prices drop,
eat more to maintain your value.

If you notice in the output the user has to enter input before the first line prints. Why is this?

BlankQQ
  • 41
  • 4
  • 2
    That shouldn't happen. Are you 100% sure the code you're running is the one we see? – fredoverflow Dec 18 '15 at 23:31
  • Are you redirecting the output to a file or pipe, by any chance? Usually, that's a problem with buffering, but when the input and output are a terminal, the standard I/O library normally flushes stdout before reading from stdin, so it normally just works. However, if the writing is going to a pipe or file, then what you're seeing is more nearly normal. Also, is there a reason pure C code is tagged as C++? C++ uses `` etc for preference. This should, presumably does, also compile as C++, but it is using legacy C features rather than C++ features. – Jonathan Leffler Dec 18 '15 at 23:33

1 Answers1

5

I tried your program and it worked as you want for me. You might try flushing the stdout buffer before you scan for input.

fflush(stdout); // right before your scanf line.

The OS is free to postpone writing output buffers for efficiency. So, for instance, you write to the disk in blocks instead of one character at a time. fflush forces the buffer to be written. It's "endl" in C++ and fflush is the straight c version. I'm not certain that's what you're seeing.

John Pretz
  • 153
  • 1
  • 8
  • Cool. You don't want to add flush everywhere now. It's only when you want to insist that you get all the output. Generally, postponing writing buffers is a good thing. – John Pretz Dec 18 '15 at 23:45
  • 1
    `stdout` should be flushed automatically when the program prints a newline. So only the line `Please enter...` should need an explicit flush. Can you explain why the other lines aren't being shown until he enters something? – Barmar Dec 19 '15 at 00:02
  • I think it's very much an implementation-dependent thing. @BlankQQ's example worked just as (s)he wanted on my machine. There is a more extensive discussion of the question here: [link](http://stackoverflow.com/questions/2123528/does-reading-from-stdin-flush-stdout) It seems that implementations often flush stdout when writing to the terminal output, but it's not guaranteed. – John Pretz Dec 19 '15 at 00:53