2016-10-16 27 views
-1

所以基本上我想讀一個文本文件,其中包含代表和拍賣的數字,有多少物品正在銷售,有多少商品以及實際商品。我必須做的是輸出最高報價作爲售價,但由於某種原因,我的計劃只打印出最後價格而不是最高價格。我的輸出是Wonky

#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 

回答

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循環等)。此外,它可能看起來更乾淨,但我個人建議圍繞一行語句使用大括號,如for/if。如果您以後需要擴展它,它們就會在那裏,但是如果您正確對齊,它也可以提高可讀性...

  • 可以在您使用的範圍內聲明變量,而不是在頂部。這些日子的編譯器足夠聰明,可以優化初始化,所以你不必擔心在循環中創建100500 int s,並且預先確認預留空間也不是太擔心。這是別的要保持一致的 - 所以如果你堅持把所有範圍的聲明放在函數的頂部,那麼就用一切來做。

  • 特別是因爲你是一個初學者 - 治療編譯器警告像錯誤(實際上編譯器有標誌自動做到這一點,迫使你修復它們)。它會爲你節省一些代碼中的問題(比如將一個迭代整數與一個指針進行比較)。

  • 你得到文件名的方式是一種被認爲是不安全的方法(scanf)。

而是執行此操作:

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

使用fgets獲取用戶輸入從標準輸入了一個警告 - 它包括從按下ENTER鍵\n換行符。而就在該符號終止字符串:

unsigned len = strlen(filename)-1; 
filename[len] = '\0'; 
  • 當打開一個文件,做一些錯誤處理,以防fopen返回null。如果它沒有正確打開,那麼這個程序中沒有其他任何東西可以發生,因爲它全部依賴於文件中的內容,並且你也會拋出一個空指針。

  • 最後,使用int來存儲整數。 float的作品,但浮點數可以......變幻莫測。 numberBids[15],bids[10]numberAuc都可以並且應該是整數。

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; 

} 
相關問題