Stuck in an inifinite loop?

This is a discussion on Stuck in an inifinite loop? within the C Programming forums, part of the General Programming Boards category; I think I'm stuck within the first loop or I have no idea what the problem is. TEXT FILE Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Stuck in an inifinite loop?

    I think I'm stuck within the first loop or I have no idea what the problem is.

    TEXT FILE
    Code:
    5
    3.00
    3.50
    4.50
    5.00
    6.00
    3
    1 1 1 1 1
    0 0 2 1 6
    1 0 3 2 3
    CODE

    Code:
    <#include <stdio.h>
    
    int main (void) 
    {
        int numOfPizzaPrices;
        int i, a;
        double pizzaPrices[i];
        int numOfOrders;
        int day;
        int pizzaOrder[i][a];
        int sum = 0;
        
        FILE *ifp;
        ifp = fopen("pizza.txt", "r");
        
        fscanf(ifp, "%d", &numOfPizzaPrices);
        
        for(i = 0; i < numOfPizzaPrices; i++)
        {
            fscanf(ifp, "%.2f", &pizzaPrices[i]);
        }
    
        fscanf(ifp, "%d", &numOfOrders);
        for (day = 1; day < numOfOrders; day++)
        {
            for(i = 0; i < numOfPizzaPrices; i++)
            {
                for(a = 0; a < numOfPizzaPrices; a++)
                {
                      fscanf(ifp, "%d", &pizzaOrder[i][a]);
                      sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);
                }
            }
            printf("On day %d, you will spend $%d at Lazy Moon.", &day, &sum);
        }
        
        system("PAUSE");
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The variable i is uninitialized, so, assuming you are compiling as C99, which otherwise you can't do what you're trying to in the first place, you're ending up with some random value for the size of i. The same goes for a.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    thanks, worked fine. but now my calculation is wrong on

    sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);

    i changed my printf line and removed the &'s.

    for the calculation, it should read the text file and i asumme the use of [i][a] will add the values inside them. such as [1][4] = 6?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Swap A and I on your two inner loops. This:
    Code:
    foo[ X ][ Y ]
    Is considered to be: X rows of Y elements. Also, you're starting one of your loops at 1.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by quzah View Post
    Swap A and I on your two inner loops. This:
    Code:
    foo[ X ][ Y ]
    Is considered to be: X rows of Y elements. Also, you're starting one of your loops at 1.


    Quzah.
    i tried
    Code:
        for (day = 1; day < numOfOrders; day++)
        {
            for(a = 0; a < numOfPizzaPrices; a++)
            {
                for(i = 0; i < numOfPizzaPrices; i++)
                {
                      fscanf(ifp, "%d", &pizzaOrder[i][a]);
                      sum = sum + (pizzaOrder[i][a] * pizzaPrices[i]);
                }
            }
            printf("On day %d, you will spend $%.2f at Lazy Moon.\n", day, sum);
        }
    if thats what you meant, sorry if i'm misunderstanding you.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    3
    1 1 1 1 1
    0 0 2 1 6
    1 0 3 2 3
    You've already read the columns above as numofprices. So now you read the number of rows as numoforders. I guess that's what you're doing anyway. So then...
    Code:
    for each row
        for each order
            read one value into x[ row ][ column ]
    Now at the end, I assume you're going to do something like:
    Code:
    for each row
        for each column
            print price[ column ] * x[ row ][ column ]
    Something like that?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    its kinda hard to explain without giving you the whole problem, but thats a bit to read i dont want you to do.

    Each of these lines will contain n non-negative integers, representing how many of those pizzas, respectively are in the order. (For example, if n=5 and the line contains the values 0 0 6 0 9, then the order contains 6 slices of pizza #2 and 9 slices of pizza #4.)

    from the problem, if that doesn't help, want me to post the whole problem?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I pretty much just described it in my previous post. Read a bunch of prices. Read a bunch of days, and how much of each price was ordered on each day. Figure out how much the cost was for each item on a day, or the grand total, or what not. Look at my previous post, that covers what you're wanting to do.

    I find it easier if I name my row and column variables as row and column, rather than just A or I. It helps me sort it out in my head easier.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 03:20 PM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. Help! Stuck in a loop!
    By raell in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2003, 10:47 AM
  5. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21