我正在做一些C类入门的作业,在这里我们必须编写一个程序,从包含来自酿酒厂的订单信息的文本文件读取输入。我已经写完了所有内容,但是当我运行它时,唯一正确打印的是“Winery#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;
}
我看了一些关于这个网站的其他的答案,但他们都不似乎帮助我与我的问题。我感到沉痛的感觉,答案是看着我的脸,但作为疲惫的业余编码员,我一直无法找到它。提前致谢!
什么是第54行? – 2012-03-27 01:47:28
'prices'没有被声明为一个数组,既不是'orders'就这件事 – GWW 2012-03-27 01:48:07
既然你称自己是一个业余爱好者,一些编码技巧:首先,我希望每个人的代码都清楚并且很好的评论!当他们描述你的推理时,你会发现评论是最有用的,而不是代码本身。假设任何读你的代码的人都理解语言是安全的。他们无法做的是看到你的想法。记录你的假设和期望也是一个好主意,例如,“只有当我们的酒已经用完时,我们才会到达这里。” – 2012-03-27 01:55:24