Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:22:19+00:00 2026-06-01T00:22:19+00:00

I’m working on some homework for an intro to C class, in which we

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:22:21+00:00Added an answer on June 1, 2026 at 12:22 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I need to write an AS3 program to search for a certain keyword in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.