简体   繁体   English

用 printf 和 scanf 用 c 编写没有按预期工作

[英]writing in c with printf and scanf not working as expected

So I'm a total newb to C. I'm using eclipse with MinGW compiler.所以我完全是 C 的新手。我正在使用 Eclipse 和 MinGW 编译器。 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.我在使用 scanf 和 printf 函数的第二章中,我的程序正在运行,但只有在我将三个整数输入 scanf 函数后才将语句打印到控制台。

#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:我期待它printf,然后scanf 为用户输入像这样:

Enter the box length: (waits for user int input; ex. 8 + "Enter")
Enter the box width: ...

Just add fflush(stdout);只需添加fflush(stdout); after each printf() before you call scanf() :在调用scanf()之前的每个printf()之后:

#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;
}

Dealing with Dirty Buffers in C !!在 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' )您可以简单地在每个 printf() 的末尾包含一个换行符(转义序列)'\\n' ,这用于刷新缓冲区,最终启用输出终端上的显示。(相同的功能由 fflush(stdout) 实现) 但是不需要每次调用 printf() 时都写它,只需包含一个字符 '\\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.注意:始终建议使用 '\\n' 字符作为 printf() 引号 "" 内的最后一个元素,因为除非使用刷新机制,否则数据将保留在缓冲区内,但是缓冲区会在出现以下情况时自动刷新main() 函数结束,此外,只有在刷新临时缓冲区时,数据才能到达目的地。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM