Thread: Help with while loop

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    7

    Question Help with while loop

    I am trying to write the beginning part of a program that has to read an array with 10 columns and the rows are not known but they are less than or equal to 150. The program is supposed to exit when a sentinal value of -1 is entered into the first column of whatever row the program is currently reading from. However, I cannot get the while loop to work and was wondering if I could get any help, here is the code I have so far:

    Code:
    #include <stdio.h>
    void ReadArray(int[][10], int, int);
    
    void main(void)
    {
    int num[3][10];
    ReadArray(num, 150, 10);
    }
    void ReadArray(int num[][10], int numrow, int numcol)
    {
    int r, c;
    
    	for (r=0;r<numrow;r++)
    	{
    
    		for (c=0;c<numcol;c++)
    		{	 
    			while (num[r] != -1)
    			{
    		scanf("%d", &num[r][c]);
    			}
    		}
    	}
    	for (r=0;r<3;r++)
    	{
    		for (c=0;c<10;c++)
    		{
    		printf("%d ", num[r][c]);
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I am trying to write the beginning part of a program that has to read an array with 10 columns and the rows are not known but they are less than or equal to 150.
    Code:
    void main(void)
    {
    int num[3][10];
    Well I guess then 3 won't be enough, will it? Oh, and go read the FAQ on why void main is wrong.

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

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    The 3 was a typo, thanks for pointing that out though, it should read:

    Code:
    #include <stdio.h>
    void ReadArray(int[][10], int, int);
    
    int main(void)
    {
    int num[150][10];
    ReadArray(num, 150, 10);
    }
    Last edited by brett73; 11-30-2004 at 01:54 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (num[r] != -1)
    			{
    		scanf("%d", &num[r][c]);
    			}
    Why are you doing this?

    First off, even if it was right, you'd be looping until they entered "quit", in effect.
    Second, you should be checking the row and the column, not just the row. Because just indexing the row gives you a pointer, which will not ever equal -1.
    Third, even if this did work right, you still wouldn't be breaking out of your loop when -1 was entered. The for loops would still merrily chug along.
    Fourth, why are you passing arbitrary numbers to control you second set of loops?


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

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    Ok, I see what you are saying, but is there any way that I could get it to exit the loop using the while (num[r][1] != -1) because i've been trying it here for a long time and I cannot get anything to work, any help would be greatly appreciated

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about not sticking the "quit or not" value in first?
    Code:
    while( somevar != -1 )
    {
        ...read into some var...
        if( somevar != -1 )
        {
            ...put this value into wherever you want it put...
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    7
    ok, I will try that, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM