whats the deal with break ?????

This is a discussion on whats the deal with break ????? within the C Programming forums, part of the General Programming Boards category; Hi ! I am newbie c programmer trying to understand some concepts. Here is a program i wrote which connects ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    whats the deal with break ?????

    Hi !

    I am newbie c programmer trying to understand some concepts. Here is a program i wrote which connects two pairs on integers in an array

    for eg if u enter 2 3 i will link the two nodes
    lets say u enter 3 4 next then i will link 3-4 and also 2-4 since 2 is already linked to 3.


    PROBLEM :

    I am confused over the break in line 33, when i enter a choice n the program should exit the while loop and display the changed array but infact it exits main() ??????????

    maybe my concepts on break are wrong can anyone help me on this ????



    Here is the code :

    Code:
    #include<stdio.h>
    
    /*  ******************************************************************************************************** */
    /*  This program creates new connection pairs b/w two elments and ignores if a connection (direct or indirect)
        already exists */
    /*  ******************************************************************************************************** */
    
    
    #define size 10
    #define TRUE 1
    
    
    int main()
    {
        int i, p, q, t, id[size];                                         /* vairables used for spanning the array */
        char c;                                                           /* variable used to store the users choice to continue */
        
        for(i = 0; i < size; id[i] = i, printf("%d ", id[i]), i++);       /* initialize array to index and print it */   
        printf("\n");
        
        while(TRUE)
        {
                scanf("%d %d", &p, &q);                                   /* read two entries and check if they are connected */
                if(id[p] == id[q])
                {
                         printf("node %d-%d already exists\n", p, q);     /* if they are connected then ask for choice from user */
                         printf("do you wish to continue : y/n");
                         fflush(stdin);                                   /* flush newline in input buffer from scanf() */
                         c = getchar();
                         if('y' == c)
    						 continue;
                         else
                             break;
                }
                else                                                      /* if not connected, connect them and all elements */
                {                                                         /* connected to them as well */
                    for(t = id[p], i = 0; i < size; i++)
                          if(id[i] == t)
                                   id[i] = id[q];
                    printf("%d-%d node created\n", p, q);                 /* print the new node created and the new array */
                    for(printf("new arrary is : "),i = 0; i < size; printf("%d ", id[i]), i++);
                    printf("\n");
                }
        }
        
        for(printf("new arrary is : "),i = 0; i < size; printf("%d ", id[i]), i++);
        printf("\n");             
        return 0;
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    477
    [EDIT]:
    removed incorrect information.
    [/EDIT]

    Also search the FAQ for why using fflush(stdin); is bad.
    Last edited by nonpuz; 03-29-2005 at 02:39 PM.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is the output I had -- what did you expect to be different?
    C:\Test>test
    0 1 2 3 4 5 6 7 8 9
    2 3
    2-3 node created
    new arrary is : 0 1 3 3 4 5 6 7 8 9
    3 4
    3-4 node created
    new arrary is : 0 1 4 4 4 5 6 7 8 9
    3 4
    node 3-4 already exists
    do you wish to continue : y/nn
    new arrary is : 0 1 4 4 4 5 6 7 8 9
    [edit]getchar returns an int.
    Quote Originally Posted by nonpuz
    break; will break out of one level of nesting...meaning
    Code:
    while (something)
      if (something)
        break;
    will break out of the while, however this:
    Code:
    while (something)
      if (something)
        if (something)
          break;
    Will break out of the first if statement and return to the while statement.
    No.
    A break statement terminates execution of the smallest enclosing switch or iteration statement.
    Last edited by Dave_Sinkula; 03-29-2005 at 03:04 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    hi !

    the program behaves different when run from dos prompt and when run from dev-C++ compiler,

    on the dev-C++ compiler when i enter a choice 'n' it just exits and does not display the final array instead the new window just closes ?????

    oh and whay is fflush() bad and what could i used instead of that ???

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by gemini_shooter
    the program behaves different when run from dos prompt and when run from dev-C++ compiler,

    on the dev-C++ compiler when i enter a choice 'n' it just exits and does not display the final array instead the new window just closes ?????
    FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
    FAQ > How do I... (Level 1) > How do I get my program to wait for a keypress?

    Quote Originally Posted by gemini_shooter
    oh and whay is fflush() bad and what could i used instead of that ???
    FAQ > Explanations of... > Why fflush(stdin) is wrong
    FAQ > How do I... (Level 2) > Flush the input buffer
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    hi !

    thanks for the links about the windows console,

    i found a way to flush without using fflush as suggested by the faq but unfortunately i didn't really understand the explantion of why fflush(stdin) is bad, if someone could give me a simpler explanation, I am pretty new at this , sorry !!!!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by gemini_shooter
    i didn't really understand the explantion of why fflush(stdin) is bad
    fflush(stdin) is undefined behavior, just like integer overflow: it might appear to do exactly what you want, it might not.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  2. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

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