-1

When I try to run this code it just runs indefinitely. I have tried to use scanf_s but that did not change anything.

#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*width*length;
    weight=(volume+165)/166;

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

    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    I've run this on both *nix & Windows; each time it prompted me for 3 inputs and printed 2 outputs. It did hang if I hit 'enter' without providing a number, but after I provided a number and hit enter, it continued. Does it prompt you for information in your console? – jhelphenstine Jun 19 '19 at 22:53
  • 1
    If it does not prompt for information you can add `fflush(stdout);` after each prompt. Note it does not hang if you hit 'enter' without providing a number. It is ignoring whitespace until you enter a number. – Weather Vane Jun 19 '19 at 23:01
  • 1
    If you can't be bothered to check the return value or results of your `scanf`, why should we bother to help you? – Lee Daniel Crocker Jun 19 '19 at 23:04
  • 1
    Please do some research on the behaviour of `scanf` and what happens if you provide alphabetic input when an integer is expected. – Weather Vane Jun 19 '19 at 23:09
  • Have you tried entering 3 numbers? – M.M Jun 19 '19 at 23:51
  • You're not validating your input. Also clean your input stream if an invalid type is entered in the stream. What I suggest is taking data as a string with fgets and performing the conversion. – Irelia Jun 20 '19 at 00:13

3 Answers3

1

The first print in your code (and similarly the others):

printf("enter height of box: ")

By default, this line will put the text into a buffer. The buffer is written to terminal ("flushed") only if either one of the following happens:

  1. The buffer gets full (can be 1024 bytes, 16384, or any other number).
  2. There is a newline at the end of the text (assuming the output is to the terminal).

    Note that this is a common behavior, but not guaranteed by the standard. Read Is stdout line buffered, unbuffered or indeterminate by default? to cover this subject.

  3. Some C implementations will automatically flush stdout when you read from stdin, but many implementations don't.

    Read Does reading from stdin flush stdout? to cover this subject.

So you can see your prompt if you change the line to:

 printf("enter height of box: \n");

A second option, is to force a flush, so that the buffer is written to the output:

 printf("enter height of box: ");
 fflush(stdout);

A third option is to disable buffering completely. For that, you can use setbuf:

void setbuf( FILE* stream, char* buffer );
  • buffer -
    pointer to a buffer for the stream to use. If NULL is supplied, the buffering is turned off. If not null, must be able to hold at least BUFSIZ characters

So, simply add the following at the beginning of the program, and all stdout buffering will be turned off:

setbuf(stdout, NULL);
Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
0

there doesn't seem to be any mistakes in the code. i tried running it on Ubuntu And OnlineGDB. The Code is working just fine. It Can be your compiler's fault.

You should check what "scanf" is returning. you can check it using this:- (here d is your integer)

if (scanf("%d", &d) == 1)
    ...all OK...
else
    ...EOF or conversion failure...

If you have several conversions, check that they all completed.

Blue Dice
  • 155
  • 1
  • 10
0

There may be a problem cause you might not be entering any value as input.

In all other cases, the code seems to work fine.

Meet Maheshwari
  • 140
  • 2
  • 9