2

I'm trying to execute a program from the book by K. N. King "C Programming, A Modern Approach, 2 Edition", in NetBeans 8.0.2, using MinGW compiler. The program looks like this:

#include <stdio.h>
int main(void) {
int height, length, width, volume, weight;

printf("Enter height of box: ");
scanf("%d", &height);
printf("Enter length of box: ");
scanf("%d", &length);
printf("Enter width of box: ");
scanf("%d", &width);
volume = height * length * width;
weight = (volume + 165) / 166;

printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n", weight);

return 0;
}

So, I press "Run Project", the program builds successfully without any errors, then it begins to run and nothing happens, no console or something like that appears to input data like height, length, etc.

In the output window, after the program builded, it begins to run and nothing is displayed in the window except for a thick cursor. When I press any button being in that window, the program ends and this is what is displayed:

Enter height of box: Enter length of box: Enter width of box: Volume (cubic inches): 0 Dimensional weight (pounds): 0
RUN SUCCESSFUL (total time: 6s)

I'm completely new to C, so I don't know, what should appear when the program runs. When launching programs written in Python the console appeared, and now I'm confused.

When I tried to debug, it builded successfully again and then the console appeared with the line: "Enter height of box: ", so it worked like I thought it should work, but I don't think this is the proper way to run programs.

rombez
  • 2,047
  • 2
  • 16
  • 16
  • Unsure for mingw, but when using Java, the *output* window is where the interactions (output and input) take place. – Serge Ballesta Feb 04 '15 at 21:55
  • Yes, have a look at the Output window and see if there is any error. – Quentin Feb 04 '15 at 22:00
  • the dimensions must be positive values, so suggest using unsigned int for variable definitions and %u for the scanf()s. suggest calling fflush(stdout); after each call to printf(). Use short while loops to force the inputs to be > 0 otherwise the weight calculation will output 0. check the returned value from the calls to scanf() to assure the input/conversion operation was successful – user3629249 Feb 04 '15 at 22:25
  • 1
    You can look [here](http://stackoverflow.com/a/27554843/1322642) . Maybe it's the reason. – moskito-x Feb 04 '15 at 22:45
  • @moskito-x yes, that solved the problem entirely, gratitude. One last thing: should I use external terminal for all the programs written in C or just those ones with scanf() statement in them? – rombez Feb 04 '15 at 22:59
  • 1
    It' easier to use it for all C Programs. – moskito-x Feb 04 '15 at 23:02
  • 1
    BTW , if you use `External Terminal` you don't need `fflush()` or `'\n'` . – moskito-x Feb 04 '15 at 23:08

1 Answers1

0

stdio streams are buffered. You need to fflush() stdout before using stdin, if you want the output to be displayed before the program blocks waiting on input. Alternatively, you can end all output lines with '\n', as newline implicitly flushes the output.
Edit: If you're entering data that scanf() cannot match using %d, such as alphabetic characters, scanf() will fail. You can check for this failure by comparing the return value of scanf() with the number of matches you expected (one in this case). As it stands, you don't check for this, and the characters stay in the input buffer, where the next scanf() will again try and fail to match them.

EOF
  • 6,273
  • 2
  • 26
  • 50
  • Okay, so it worked, I wrote `fflush(stdout)` to all 3 instances of input and a line: `Enter height of box: ` appeared in the output window. But when I press any number to input a value, it doesn't work, nothing appears. When I press Enter, the next line appears and after the third line the program stops, failing to accept any input. – rombez Feb 04 '15 at 22:28