Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing in c with printf and scanf not working as expected

Tags:

c

printf

scanf

So I'm a total newb to C. I'm using eclipse with MinGW compiler. I'm on the second chapter using the scanf and printf functions and my program is working, but only printing the statements to the console once I've entered the three ints into the scanf functions.

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

    printf("Enter the box length: ");
    scanf("%d", &length);
    printf("\nEnter the box width: ");
    scanf("%d", &width);
    printf("\nEnter the box height");
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}

console output:

8 (user input + "Enter" + key)
10 (user input + "Enter" key)
12 (user input + "Enter" key)
Enter the box length: 
Enter the box width: 
Enter the box heightDimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6

any insights? I'm expecting it to printf, then scanf for user input like so:

Enter the box length: (waits for user int input; ex. 8 + "Enter")
Enter the box width: ...
like image 858
ChrisMcJava Avatar asked Sep 05 '25 03:09

ChrisMcJava


2 Answers

Just add fflush(stdout); after each printf() before you call scanf():

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

    printf("Enter the box length: "); fflush(stdout);
    scanf("%d", &length);
    printf("\nEnter the box width: "); fflush(stdout);
    scanf("%d", &width);
    printf("\nEnter the box height"); fflush(stdout);
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}
like image 183
phonetagger Avatar answered Sep 07 '25 19:09

phonetagger


Dealing with Dirty Buffers in C !!

You can simply include a newline character (escape sequence) '\n' at the end of each printf(), this serves for flushing the buffers, that eventually enable the display on the output terminal.( same functionality is achieved by fflush(stdout) but there is no need to write it every time you call the printf(), just include a character '\n')

Note: Its always recommended to use a '\n' character as the last element inside the quotes "" for printf(), as the data will stay within the buffers unless a flushing mechanism is used, however the buffers are flushed automatically when the main() function ends, Moreover, data reaches its destination only when the interim buffers are flushed.

Our new code should look like this:

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;
    printf("Enter the box length: \n");
    scanf("%d", &length);
    printf("\nEnter the box width: \n");
    scanf("%d", &width);
    printf("\nEnter the box height \n");
    scanf("%d", &height);
    volume = length * width * height;
    dweight = (volume + 165) / 166;
    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);
    return 0;
}

Console output :

Enter the box length: 
8
Enter the box width:  
10
Enter the box height 
12
Dimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6
like image 23
Sankalp Negi Avatar answered Sep 07 '25 21:09

Sankalp Negi