I’m working on some homework for an intro to C class, in which we must write a program that reads input from a text file that contains order information from a winery. I’ve got everything written out, but when I run it, the math for the total cost of the orders is off. I got some help with fixing the arrays in this program from some users last night, but now I’m not quite sure what is causing the math error. This is the code I have:
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;
}
When I run the program, the following prints out:
Winery #1
Order #1: $150
Order #2: $60
Order #3: $60
Order #4: $0
Winery #2
Order #1: $0
When it SHOULD print out:
Winery #1
Order #1: $160
Order #2: $200
Order #3: $120
Order #4: $40
Winery #2
Order #1: $40
This is the data from the input file "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
And in case it’s necessary (I figure it’s entirely possible that my math error could be pointed out through only the code, but I want to include this for clarity), this is my professor’s explanation of what each integer in the input file represents:
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.
I apologize for the length of the question, I just want to make sure I give all the information necessary in order for clarity. As always, I greatly appreciate the help, as you all at stackoverflow continue to make my much easier.
Check your placement of braces (
{,}) for loops — if there are no braces around the body of a loop, then only the next statement is executed. Your indentation suggests that you expect several following statements to execute, but unlike python, indentation is not significant in C