Thread: break in for loop not working

  1. #16
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    C code is quite hackable, isn't it?

    One wrong move and a black hole sprouts before you!
    Devoted my life to programming...

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    C code is quite hackable, isn't it?

    One wrong move and a black hole sprouts before you!
    Yep... but I didn't much mind the trip to Alpha Centauri... they have excellent coffee

  3. #18
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Question

    Code:
    matrix[10][10]={0}; //initial declaration of my matrix
    int x = 'x';
    
    for(i = 0; i <10; i++){
           for(j = 0; j<cols; j++){
            scanf("%d", &matrix[i][j]);
            if(matrix[i][j] == x)
               break;
                     
                 }
         }
    it doesn't matter how I do it, the 'i' counter never stops. Even if I moved the if statement after the following bracket, it will just make it worse. When I compile and then run it it gives me extra values (0's in this case). For example I set 'j' =2. Then I enter 4 values, 1 through 4. It would give me 1,2, 3, 4, 0, 0, 0, 0, 0,0

  4. #19
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by torquemada View Post
    it doesn't matter how I do it, the 'i' counter never stops. Even if I moved the if statement after the following bracket, it will just make it worse. When I compile and then run it it gives me extra values (0's in this case). For example I set 'j' =2. Then I enter 4 values, 1 through 4. It would give me 1,2, 3, 4, 0, 0, 0, 0, 0,0
    Did you even read our posts so far?
    Devoted my life to programming...

  5. #20
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by Sipher View Post
    C code is quite hackable, isn't it?

    One wrong move and a black hole sprouts before you!
    So true, so true...

    Code:
    int main(void)
    {
    	const size_t rows = 4, cols = 3;
    	int mat[rows][cols] = {{0}};
    	int* mb = ( int*)mat, * me = mb + sizeof(mat) / sizeof(int);
    	while(mb != me)
    		if(scanf("%d", mb++) != 1)
    			break;
    	for(size_t r = 0; r < rows; ++r)
    		for(size_t c = 0; c < cols; ++c)
    			printf("mat[%d][%d]: %d\n", r, c, mat[r][c]);		
    }

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    Did you even read our posts so far?
    Naaa... he waited for gardhr to post a working solution so he could cut and paste it into his own code...

  7. #22
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Quote Originally Posted by CommonTater View Post
    Naaa... he waited for gardhr to post a working solution so he could cut and paste it into his own code...
    Well, nope. If I were waiting just for the code I would have just asked for it since the beginning. But that's no the point if I won't learn programming >_> newbie

    Quote Originally Posted by Sipher View Post
    Did you even read our posts so far?
    Yes I have, I tried putting the break within just the first for loop and it didn't work. I tried testing for the output/input but I didn't get it right either. I got rid of the if(isalpha()) because like said before, I don't need it. I know that for loops are intended to go through a number of iterations. Just a question to get out of the doubt. Would a while loop be better in this case?

  8. #23
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by torquemada View Post
    Just a question to get out of the doubt. Would a while loop be better in this case?
    Nope, I don't think so. You see, the equivalent of:
    Code:
    for (i = 0; i < 10; i++){
        ....;
    }
    is as loop:
    Code:
    i = 0
    while (i < 10)
    {
        ....;
        
        ++i;
    }
    Which one of the above looks more readable to you?
    Devoted my life to programming...

  9. #24
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    ok, the problem is stopping the 'i' from going all the way to ten. I tried breaks in different places. If I put it within just the 'i' for loop, it won't let me enter enough numbers.
    I put a printf statement to print 'i'. If I do the following, this happens
    Code:
    for(i = 0; i < 10; i++){
           for(j = 0; j<cols; j++){
             scanf("%d", &matrix[i][j]);
                }
             if(matrix[i][j] != 1)
               break;
           }
    when run with gcc,
    Code:
     
    enter the number of columns you want your matrix to be
    the number can't exceed 10: 2
    you can enter up to 20 items:
    please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter
    enter a non numeric value to stop entering values
    1
    2
    
    i = 0
    
    ***this is your matrix***
    (doesn't display anything)
    this what happens when I put the break before
    Code:
    enter the number of columns you want your matrix to be
    the number can't exceed 10: 2
    you can enter up to 20 items:
    please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter
    enter a non numeric value to stop entering values
    1
    2
    3
    4
    x
    
    i = 10
    ***this is your matrix***
      1  2
      3  0
      4  0
      0  0
      0  0

  10. #25
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by GReaper View Post
    Simple: a "break" breaks only the loop that is in, no other. Here you have two loops!

    EDIT: Use something like:
    Code:
    for (i = 0; i < 10; i++){
        for (j = 0; j < 10; j++){
            // Break however you want in here
        }
    
        if (j < 10)
            break;
    }
    That is of course only one of the ways to do it.
    Quote Originally Posted by dylan View Post
    Just before the break set the i to 10 (or greater).
    Then the first for loop will stop.

    Dylan
    Do you see why I asked you if you read our posts? Make your pick.
    Devoted my life to programming...

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @torquemada...

    Did you even try the suggestion of making it into a function and using return to exit both loops?

  12. #27
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    Quote Originally Posted by GReaper View Post
    Just before the break set the i to 10 (or greater).
    Then the first for loop will stop.

    Dylan
    I tried it but the problem is that it won't let me enter numbers pass 2. It might not be the best implementation.
    Code:
    
       for(i = 0; i < 10; i++){
           for(j = 0; j<cols; j++){
             scanf("%d", &matrix[i][j]);
             if(matrix[i][j] != 1)
              i = 10; 
            }
          
          if(i >= 10)
            break;
          }
    output
    Code:
    enter the number of columns you want your matrix to be
    the number can't exceed 10: 2
    you can enter up to 20 items:
    please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter
    enter a non numeric value to stop entering values
    1
    2
    (it won't let me enter more numbers)
    
    ***this is your matrix***
      1  2
      0  0
      0  0
      0  0
      0  0

  13. #28
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Nonono... you mixed it all up! Stop, rewind and go back to your first code posted. There apply only one of the methods we told you.

    EDIT: I just noticed: Don't check the matrix's value, check the return value of scanf. If it's zero, then do something to escape from the loops.
    Last edited by GReaper; 09-18-2011 at 11:56 AM.
    Devoted my life to programming...

  14. #29
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Cool

    Sorry for the late reply.
    I just wanted to say thanks to every one for their time

    I figured something out
    Instead of letting 'i' be my counter I created another variable "counter" to count 'i' but it wouldn't go higher once the break is reached in the loop. So I stored that value and used it as a counter. It might not sound like the best idea to you but it works for me.
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to break out of loop
    By Ducky in forum C++ Programming
    Replies: 31
    Last Post: 09-07-2010, 11:22 PM
  2. break from for loop
    By taurus in forum C Programming
    Replies: 3
    Last Post: 09-24-2009, 03:54 PM
  3. how could I break this loop?
    By Trafalgar Law in forum C Programming
    Replies: 4
    Last Post: 09-27-2008, 05:11 AM
  4. Loop will not break.
    By silhoutte75 in forum C Programming
    Replies: 1
    Last Post: 03-20-2008, 06:51 PM
  5. Need help with break in do while loop
    By JoelearningC in forum C Programming
    Replies: 3
    Last Post: 03-19-2008, 11:12 PM