C:使用输入和数组时出现数学结果问题。

标签 c arrays io

我正在为 C 类的介绍做一些家庭作业,其中我们必须编写一个程序,从包含来自酒厂的订单信息的文本文件中读取输入。我已经把所有的东西都写出来了,但是当我运行它时,订单总成本的数学计算不对了。昨晚我从一些用户那里得到了修复这个程序中的数组的一些帮助,但现在我不太确定是什么导致了数学错误。这是我的代码:

int main () {

//Creates the file pointer and variables
FILE *ifp;
int index, index2, index3, index4;
int wineries, num_bottles, prices, orders, 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[num_bottles]);

    //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[num_bottles] * total_orders);


        printf("Order #%d: $%d\n", index3+1, sum_order);
        }
        printf("\n");

    }
    //printf("%d", prices[index2]);
    fclose(ifp);


return 0;
}

当我运行程序时,打印出以下内容:

Winery #1

Order #1: $150

Order #2: $60

Order #3: $60

Order #4: $0

Winery #2

Order #1: $0

什么时候应该打印出来:

Winery #1

Order #1: $160

Order #2: $200

Order #3: $120

Order #4: $40

Winery #2

Order #1: $40

这是来自输入文件“wine.txt”的数据:

2
3
10 20 30
4
1 0 5
8 3 2
2 2 2
4 0 0
5
17 27 44 54 75
1
1 2 0 0 0

如果有必要(我认为完全有可能仅通过代码指出我的数学错误,但为了清楚起见,我想将其包括在内),这是我的教授对输入文件中每个整数的解释代表:

The first line of each test case will contain a single positive integer, k (k ≤ 10), representing the number of different possible bottles of wine from that winery. The second line of each test case will contain k positive integers representing the cost of each type of bottle of wine at that winery, separated by spaces. The third line of each test case will contain a single positive integer, c (c < 10), representing the number of orders from that winery to process. The following c lines will contain k integers each, representing the number of bottles of each type for that order.

对于问题的长度,我深表歉意,我只是想确保我提供了所有必要的信息,以便清楚。与往常一样,我非常感谢您的帮助,因为 stackoverflow 的所有人继续让我的工作变得更加轻松。

最佳答案

检查循环中大括号 ({, }) 的位置——如果循环体周围没有大括号,则只执行下一条语句.您的缩进表明您希望执行以下几个语句,但与 python 不同,缩进在 C 中并不重要

关于C:使用输入和数组时出现数学结果问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9898472/

相关文章:

c - 关于解码UDP数据包的建议

c - 将 strcpy() 与结构数组(及其元素)一起使用不起作用

arrays - 在 3D 数组中查找连续值

python - 无法使用 pickle Python 写入二进制文件

c - 将数组(结构成员)传递给函数

c - 截断文件时出现段错误

java - 替换字节数组中的值会产生奇怪的结果

c - 套接字:是什么导致 read() 返回 EINVAL?

java - 字符串缓冲区 : Adding a newline after a certain amount of words for formatting

iphone - 从 NSData 读取 NSNumber