C:下标值既不是数组也不是指针

C:下标值既不是数组也不是指针,c,arrays,io,C,Arrays,Io,我正在为C类的介绍做一些家庭作业,其中我们必须编写一个程序,从包含酒厂订单信息的文本文件中读取输入。我已经把所有的东西都写出来了,但当我运行它时,唯一能正确打印的东西就是“Winery#1:”然后窗口就掉了。我试图在程序结束时打印出我的一个数组,以查看问题出在哪里,然后我得到一个错误,该错误指出: |54|error: subscripted value is neither array nor pointer| 我理解这个错误的含义,尽管我不确定我需要做什么来纠正它。我相信我已经正确地声明了

我正在为C类的介绍做一些家庭作业,其中我们必须编写一个程序,从包含酒厂订单信息的文本文件中读取输入。我已经把所有的东西都写出来了,但当我运行它时,唯一能正确打印的东西就是“Winery#1:”然后窗口就掉了。我试图在程序结束时打印出我的一个数组,以查看问题出在哪里,然后我得到一个错误,该错误指出:

|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;
}
int main(){
//创建文件指针和变量
文件*ifp;
int索引,index2,index3,index4;
国际葡萄酒厂、数量瓶、订单、价格、总订单、总订单;
//打开要从中读取的文件。
ifp=fopen(“wine.txt”,“r”);
//扫描文件的第一行,找出有多少家酿酒厂,
//从而确定循环必须重复多少次。
fscanf(ifp、%d、&酒厂);
//开始主循环,其重复次数等于酿酒厂的数量。
对于(索引=0;索引<酒厂;索引++){
//打印酒厂编号
printf(“酿酒厂#%d:\n”,索引+1);
//扫描上述酒厂的酒瓶数量,并
//创建大小为“num_瓶”的数组“prices”
fscanf(ifp,“%d”,瓶数);
国际价格[num_瓶];
//将瓶子价格扫描到数组中
用于(index2=0;index2
我在这个网站上查看了一些其他的答案,但似乎没有一个能帮助我解决问题。我有种沮丧的感觉,答案就在看着我的脸,但作为一个疲惫的业余程序员,我一直没能找到答案。提前谢谢

变化

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


有两个
prices
一个是for循环内的数组,另一个是循环外的int。因此,当您尝试在末尾打印时,
prices[num_瓶]
不再存在,因为只有int prices存在。显然,整数价格不能用作
价格[index2]


从for循环中取出价格并将其放在顶部。

prices
未声明为数组,
orders
也未声明为数组。因此,如果您称自己为业余爱好者,请提供一些编码提示:首先,我希望每个人的代码都如此清晰且注释良好!您会发现注释在描述您的推理时最有帮助,而不是代码本身。可以安全地假设,任何阅读您的代码的人都理解该语言;他们不能做的是看到你的想法。记录你的假设和期望也是一个好主意,比如“我们只在用完了酒的时候才到达这里。”用描述性的方式命名你的变量:而不是<代码>索引x2>代码>和<代码>索引3>代码>,考虑<代码>瓶子< /代码>和<代码>命令< /代码>。最后,考虑在<<代码>的单行体上使用括号为,<>代码> ,或者<代码>如果语句。这样,当您在正文中添加另一行时,您就不会导致错误,并欺骗自己认为缩进将说服编译器按照您的意思执行。:-)错误代码闻起来像C,就像整个程序一样。你确定问题是C++吗?谢谢你的提示,亚当!是的,我有一位很棒的编程老师,他告诉我们在编写代码时,清晰性是多么重要,这让我明白了这一点。
//Scans the number of bottles at the aforementioned winery and
//creates the array "prices" which is size "num_bottles."
fscanf(ifp,"%d", &num_bottles );
//               ^