在C与printf的写作和预期scanf函数不工作

在C与printf的写作和预期scanf函数不工作

本文介绍了在C与printf的写作和预期scanf函数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对C.我使用日食与MinGW的编译器总福利局。我在使用scanf函数和printf函数的第二章,我的程序工作,但只打印语句到控制台一旦我进入这三个整数到scanf函数的功能。

 的#include<&stdio.h中GT;
诠释主要(无效){
    INT长度,高度,宽度,体积,dweight;    的printf(请输入框长度:);
    scanf函数(%d个,&安培;长度);
    的printf(\\ n输入框的宽度:);
    scanf函数(%d个,&安培;宽度);
    的printf(\\ n输入框的高度);
    scanf函数(%d个,&安培;高度);    体积=长×宽×高;
    dweight =(音量+ 165)/ 166;    的printf(尺寸:L =%D,W =%D,H =%d个\\ N,长,宽,高);
    的printf(卷数:%d \\ n,体积比);
    的printf(尺寸宽度数:%d \\ n,dweight);    返回0;
}

控制台输出:

  8(用户输入+Enter键+键)
10(用户输入+Enter键)
12(用户输入+Enter键)
输入框长度:
输入框宽度:
输入框heightDimensions:L = 8,W = 10,H = 12
体积:960
尺寸宽度:6

任何见解?我期待它给printf,然后scanf函数用户的输入,像这样:

 输入框长度:(等待用户INT输入;前8 +回车)
输入框宽度:...


解决方案

只需添加 fflush(标准输出); 之后每个的printf()你打电话之前 scanf()的

 的#include<&stdio.h中GT;
诠释主要(无效){
    INT长度,高度,宽度,体积,dweight;    的printf(请输入框长度:); fflush(标准输出);
    scanf函数(%d个,&安培;长度);
    的printf(\\ n输入框的宽度:); fflush(标准输出);
    scanf函数(%d个,&安培;宽度);
    的printf(\\ n输入框的高度); fflush(标准输出);
    scanf函数(%d个,&安培;高度);    体积=长×宽×高;
    dweight =(音量+ 165)/ 166;    的printf(尺寸:L =%D,W =%D,H =%d个\\ N,长,宽,高);
    的printf(卷数:%d \\ n,体积比);
    的printf(尺寸宽度数:%d \\ n,dweight);    返回0;
}

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: ...
解决方案

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

这篇关于在C与printf的写作和预期scanf函数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 08:59