Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 文件输出越来越奇怪_C - Fatal编程技术网

C 文件输出越来越奇怪

C 文件输出越来越奇怪,c,C,在我的程序写入文本文件后,我得到了奇怪的字符。例如: Enter Weight in Pounds and Height in Inches: ÍÍÍÍYour BMI is: 25.11 超重 这些“ÍÍÍÍ”应该是数字,即:170 70 还有其他人遇到过这个问题吗?如果你需要看我的代码,就在这里 #include <stdio.h> #include <stdlib.h> FILE *fp; void Calculate_BMI(double Weight_in_

在我的程序写入文本文件后,我得到了奇怪的字符。例如:

Enter Weight in Pounds and Height in Inches: ÍÍÍÍYour BMI is: 25.11
超重

这些“ÍÍÍÍ”应该是数字,即:170 70

还有其他人遇到过这个问题吗?如果你需要看我的代码,就在这里

#include <stdio.h>
#include <stdlib.h>

FILE *fp;
void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double BMI);
int main()
{
    fp = fopen("csis.txt", "w");
    int i;
    double Wieght_in_Pounds, Hieght_in_Inches, BMI;

    Wieght_in_Pounds = 0;
    Hieght_in_Inches = 0;
    BMI = 0;
    for (i = 1; i <= 4; ++i) {
        Calculate_BMI(Wieght_in_Pounds, Hieght_in_Inches, BMI);
    }

    fclose(fp);
    system("pause");
    return(0);
}

void Calculate_BMI(double Weight_in_Pounds, double Hieght_in_Inches, double 
BMI)
{
    printf("Enter Weight in Pounds and Hieght in Inches: ");
    fprintf(fp, "Enter Weight in Pounds and Hieght in Inches: ");
    scanf(" %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
    fscanf(fp, " %lf %lf", &Weight_in_Pounds, &Hieght_in_Inches);
    BMI = (Weight_in_Pounds * 703) / (Hieght_in_Inches * Hieght_in_Inches);
    printf("Your BMI is: %3.2lf\n", BMI);
    fprintf(fp, "Your BMI is: %3.2lf\n", BMI);
    if (BMI < 18.5) {
        printf("Under Weight\n");
        fprintf(fp, "Under Weight\n");
    }
    else if (BMI >= 18.5, BMI <= 25.0){
        printf("Normal weight\n");
        fprintf(fp, "Normal weight\n");
    }
    else if (BMI >= 25.0, BMI <= 30.0) {
        printf("Overweight\n");
        fprintf(fp, "Overweight\n");
    }
    else {
        printf("Obese\n");
        fprintf(fp, "Obese\n");
    }
}
#包括
#包括
文件*fp;
无效计算体重指数(双倍体重单位为磅,双倍身高单位为英寸,双倍体重指数);
int main()
{
fp=fopen(“csis.txt”,“w”);
int i;
双倍体重单位磅,身高单位英寸,BMI;
以磅为单位的重量=0;
高度单位为英寸=0;
体重指数=0;
对于(i=1;i=18.5,BMI=25.0,BMI
  • 对以写入模式(
    w
    )打开的文件执行的操作
    fscanf()
    无效。请删除
    fscanf()
    语句


  • if
    语句中的
    运算符替换
    。逗号(
    )运算符的结果在序列中具有与其最右边的操作数相同的类型和值。因此只有
    BMI Fyi,
    if的结果(BMI>=18.5,BMI-Huh,好的,谢谢。这很奇怪,因为它根本没有给我任何警告。虽然仍然给出了正确的答案。@Anthony。是的,你很幸运,因为有
    else if
    。如果它们是多个
    if
    语句,输出会有所不同。请注意,将参数传递给函数时,这些参数是不相关的被
    scanf()额外覆盖
    函数中的调用是无意义的。函数不应带任何参数,参数应成为局部变量。您应该注意,“weight”和“height”都违反了“e之前i,c之后除外”规则,并且应该拼写一致。您应该检查
    scanf()
    fscanf()中的返回值
    。写
    scanf(“%lf%lf”、&Weight\u in\u Pounds,&hight\u in\u);fscanf(fp、“%lf%lf”、&Weight\u in\u Pounds,&hight\u in\u)没有意义;
    -您丢弃了用户输入。我知道这是一个无效的操作。但我仍然希望在接收用户输入时不显示那些奇怪的字符。我怎么做呢?因为我只知道fscanfy您必须比较
    scanf()的返回值
    针对2。如果发生,则从函数返回,否则旧值/垃圾值可能会作为输入
    if (BMI >= 25.0 && BMI <= 30.0)