我的输出是无数的

时间:2016-10-16 02:26:09

标签: c

所以基本上我想阅读一个文本文件,其中包含代表和拍卖的数字,销售的商品数量,有多少优惠以及实际优惠。我要做的是输出最高报价作为售价,但由于某种原因,我的程序只打印出最后的价格而不是最高价格。

#include <stdio.h>
//main function
int main() {
    //Declaring variables and arrays
    float   numberBids[15], max, sum = 0 ,numberAuc;
    float   bids[10];
    int     i, y, j= 0, x, z = 0;
    char    filename[100]= "";

    //User puts in filename
    printf("Please enter the name of the file.\n");
    scanf("%s",&filename);

    //Opens file
    FILE * finp=fopen(filename,"r");

    //Scans info
    fscanf(finp, "%f" , &numberAuc);

    for(i=0; i < numberAuc; i++){

    fscanf(finp, "%f",&numberBids[i]);
        for (x = 0; x < numberBids[i];x++)

        {

        fscanf(finp, "%f", &bids[i]);
        max = bids[i];
        }
    //Replaces old max with newer one if larger
    for(j; j<numberBids; j++)
        if (bids[i]>max)
        max = bids[i];

    }

    //Sum
    sum += bids[i];
    //Print out to the output
    for ( y = 0; y <  numberAuc; y++ )
    {
        y = y+ 1;
        printf("Auction %d was sold for $%.2f\n", y, bids[z]);
        z++;
        y = y- 1;
    }
    //Close
    fclose(finp);

return 0;

}

以下是文本文件的内容

5

4

100 500 250 300

1

700

3

300 150 175
2

920 680

8

20 10 15 25 50 30 19 23

2 个答案:

答案 0 :(得分:1)

您没有重新分配实际的bid金额。你在代码中有很多未使用和无意义的陈述(我会稍微更新一下),但主要的部分是你并没有真正做任何事情。 max变量。

您的代码(添加了一些评论):

for (x = 0; x < numberBids[i];x++){
    /*Why are you using the `i` variable in a loop of `x`?*/
    fscanf(finp, "%f", &bids[i]);
    /*Setting the max to each as it's read in - doesn't do anything except waste cycles*/
    max = bids[i];
}

/*
* You are comparing j to a pointer, numberBids here.
* You're saying: "while j is less than some memory address"
*/
for(j; j<numberBids; j++){
    /*
    * You aren't using the j variable anyways, so you're comparing the same 
    * two numbers here every iteration (max and bid[i], which doesn't change
    * until the next auction since you are using `i`) 
    */
    if (bids[i]>max){
        max = bids[i];
    }
}

据我所知,您的代码只需要执行以下操作:

for each auction:
    get number of bids
    get bid amounts
    get the maximum of these bids

因此代码应如下所示:

/*for each auction*/
for(int i=0; i < numberAuc; i++) {

    /*get number of bids*/
    fscanf(finp, "%f", &numberBids[i]);

    float max = 0;
    for (int x = 0; x < numberBids[i]; x++){
        /*get bid amounts*/
        fscanf(finp, "%f", &bids[x]);

        /*find the maximum*/
        max = bids[x] > max ? bids[x] : max;
    }

    printf("Auction %d was sold for $%.2f\n", i, max);
}

Full code file (with all fixes mentioned below)

其他注释(特别是代码风格/可读性):

  • 与花括号保持一致。你的一些人在下一行与其他人在同一行上与需要作用域的声明(for loops等)。此外,它可能看起来更干净,但我个人建议在像for / if这样的一行语句中使用花括号。如果您需要稍后进行扩展,那么它们会在那里进行扩展,但如果您正确对齐,它还可以提高可读性...

  • 可以在您使用它们的范围内声明变量,而不是在顶部使用所有变量。这些天的编译器非常聪明,可以优化初始化,因此您不必担心在循环中创建100500 int,并且预先确定预留空间这些天并不是很担心无论是。这是另一回事 - 所以如果你坚持在函数顶部放置所有范围的声明,那就用它来做。

  • 特别是因为你是初学者 - 对待错误的编译器警告(事实上编译器有标志自动执行此操作,强制您修复它们)。它会在代码中省去一些问题(比如将迭代整数与指针进行比较)。

  • 获取文件名的方式是一种被认为不安全的方法(scanf)。

请改为:

char filename[100];
fgets(filename, sizeof(filename), stdin);

使用fgets从stdin获取用户输入有一个警告 - 它包括按下ENTER的\n换行符。只需在该符号处终止字符串:

unsigned len = strlen(filename)-1;
filename[len] = '\0';
  • 打开文件时,如果fopen返回NULL,请执行某种错误处理。如果它没有正确打开,那么这个程序中没有任何其他内容可以发生,因为它全部依赖于文件中的内容,而且你还会抛出一个空指针。

  • 最后,使用int存储整数。 float有效,但浮点数可以......变化无常。 numberBids[15]bids[10]numberAuc都可以而且应该是整数。

答案 1 :(得分:0)

以下是代码中的一些修改,它们产生以下输出:

Auction 1 was sold for $500.00
Auction 2 was sold for $700.00
Auction 3 was sold for $300.00
Auction 4 was sold for $920.00
Auction 5 was sold for $50.00

代码创建输入文件,并且不会要求用户输入,以便进行测试。

#include <stdio.h>
//main function
int main() {
    //Declaring variables and arrays
    int   numberBids[15], numberAuc;
    float max/*,sum = 0 ,*/;
    float   bids[10];
    int     i, /*y,*/ j= 0, x/*, z = 0*/;
    char    filename[100]= "test.txt";

    FILE* ftest = fopen(filename, "w+");
    fprintf(ftest, "5\n");//Number auctions
    fprintf(ftest, "4\n");//Number bids for auction 1
    fprintf(ftest, "100 500 250 300\n");
    fprintf(ftest, "1\n");
    fprintf(ftest, "700\n");
    fprintf(ftest, "3\n");
    fprintf(ftest, "300 150 175\n");
    fprintf(ftest, "2\n");
    fprintf(ftest, "920 680\n");
    fprintf(ftest, "8\n");
    fprintf(ftest, "20 10 15 25 50 30 19 23\n");
    fclose(ftest);

    //User puts in filename
    /* We temporary skip this ...
    printf("Please enter the name of the file.\n");
    scanf("%s",filename);
    */
    //Opens file
    FILE * finp=fopen(filename,"r");

    //Scans info
    fscanf(finp, "%d" , &numberAuc);

    for(int i=0; i < numberAuc; i++){
    fscanf(finp, "%d",&numberBids[i]);
        max=0;
        for (int x = 0; x < numberBids[i];x++)
        {
        fscanf(finp, "%f", &bids[i]);
        if(bids[i]>max)max = bids[i];
        }
    //Replaces old max with newer one if larger
        /*
    for(int j=0; j<numberBids; j++)
    {
        if (bids[i]>max)
        max = bids[i];
    }*/
    printf("Auction %d was sold for $%.2f\n", i+1, max);
    }

    /*
    //Sum
    sum += bids[i];
    //Print out to the output
    for ( y = 0; y <  numberAuc; y++ )
    {
        y = y+ 1;
        printf("Auction %d was sold for $%.2f\n", y, bids[z]);
        z++;
        y = y- 1;
    }*/
    //Close
    fclose(finp);

return 0;

}