2012-03-27 7 views
0

私はワイン醸造所からの注文情報を含むテキストファイルからの入力を読み込むプログラムを書く必要がある、Cのイントロの宿題についていくつかの課題に取り組んでいます。私はすべてを書いていますが、実行すると「Winery#1:」と表示され、ウィンドウが正しく表示されません。私はこの問題は何であったか確認するために、プログラムの終了時に、私のアレイのうちの1つをプリントアウトしようとした、と私は述べてエラーを得た:C:添え字付きの値が配列でもポインタでもない

|54|error: subscripted value is neither array nor pointer| 

私はエラーが何を意味するかを理解し、私はわからないけれども私はそれを修正するために何をする必要があります。私は正しく私の配列などを宣言していると信じていますが、私はまだエラーが発生します。これは私が持っているコードです:私はこのサイト上の他の回答のいくつかを見

int main() { 
    //Creates the file pointer and variables 
    FILE *ifp; 
    int index, index2, index3, index4; 
    int wineries, num_bottles, orders, prices, sum_order, total_orders; 

    //Opens the file to be read from. 
    ifp = fopen ("wine.txt", "r"); 

    //Scans the first line of the file to find out how many wineries there are, 
    //thus finding out how many times the loop must be repeated. 
    fscanf(ifp, "%d", &wineries); 

    //Begins the main loop which will have repititions equal to the number of wineries. 
    for (index = 0; index < wineries; index ++) { 
    //Prints the winery number 
    printf("Winery #%d:\n", index + 1); 

    //Scans the number of bottles at the aforementioned winery and 
    //creates the array "prices" which is size "num_bottles." 
    fscanf(ifp,"%d", num_bottles); 
    int prices[num_bottles]; 

    //Scans the bottle prices into the array 
    for (index2 = 0; index2 < num_bottles; index2++) 
     fscanf(ifp, "%d", &prices[index2]); 

    //Creates variable orders to scan line 4 into. 
    fscanf(ifp, "%d", &orders); 

    for(index3 = 0; index3 < orders; index3++){ 
     int sum_order = 0; 

     for(index4 = 0; index4 < num_bottles; index4++) 
     fscanf(ifp, "%d", &total_orders); 

     sum_order += (prices[index4] * total_orders); 
     printf("Order #%d: $%d\n", index3+1, sum_order); 
    } 
    } 
    printf("%d", prices[index2]); 
    fclose(ifp); 
    return 0; 
} 

、それらのどれも私の問題で私を助けるように見えません。私は答えが顔で私を見ている沈んだ感じを得るが、私は疲れたアマチュアコーダーである、私はそれを見つけることができませんでした。前もって感謝します!

+0

54行とは何ですか? –

+0

'prices'は配列として宣言されておらず、どちらも' orders'ではありません – GWW

+1

あなたは自分自身をアマチュアと呼んでいるので、いくつかのコーディングのヒント:まず、皆さんのコードをこのように明快にコメントしてください!コメントは、コード自体ではなく推論を記述するときに最も役立つことがわかります。あなたのコードを読んでいる誰もがその言語を理解していると仮定するのは安全です。彼らが何をするかはあなたの考えを見るものです。また、「私たちがワインを使い果たした場合にのみここに着きます」などの前提と期待を文書化することも良い考えです。 –

答えて

0

変更

//Scans the number of bottles at the aforementioned winery and 
//creates the array "prices" which is size "num_bottles." 
fscanf(ifp,"%d", num_bottles); 

//Scans the number of bottles at the aforementioned winery and 
//creates the array "prices" which is size "num_bottles." 
fscanf(ifp,"%d", &num_bottles); 
//    ^
1

に2 pricesつがありますループと別のための内部の配列はループの外intです。したがって、prices[num_bottles]は、最後に印刷しようとしたときにそこには存在しません。そこにはint価格だけがあります。明らかに、int価格はprices[index2]として使用することはできません。

forループの内側から価格を取り出し、上部に置きます。

関連する問題