Problem with nested while

Discussion in 'C++' started by desktop, Apr 26, 2007.

  1. desktop

    desktop Guest

    I have this nested while:




    int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99};
    int* startx = numsx;
    int endx = sizeof(numsx) / sizeof(numsx[0]);
    int* endxp = numsx + endx;

    int numsy[] = { 1, 2, 3, 9};
    int* starty = numsy;
    int endy = sizeof(numsy) / sizeof(numsy[0]);
    int* endyp = numsy + endy;


    while (startx != endxp){
    std::cout << " *startx " << *startx << std::endl;

    while (starty != endyp) {
    std::cout << " *starty " << *starty << std::endl;
    starty++;
    }


    startx++;
    }

    but when I run it I get:

    *startx 11
    *starty 1
    *starty 2
    *starty 3
    *starty 9
    *startx 22
    *startx 33
    *startx 44
    *startx 55
    *startx 77
    *startx 88
    *startx 99

    Any ideas on why the inner while only gets executed once?
    desktop, Apr 26, 2007
    #1
    1. Advertising

  2. desktop

    Howard Guest

    "desktop" <> wrote in message
    news:f0r364$80g$-c.dk...
    >I have this nested while:
    >
    >
    >
    >
    > int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99}; int* startx = numsx;
    > int endx = sizeof(numsx) / sizeof(numsx[0]);
    > int* endxp = numsx + endx;
    >
    > int numsy[] = { 1, 2, 3, 9}; int* starty = numsy;
    > int endy = sizeof(numsy) / sizeof(numsy[0]);
    > int* endyp = numsy + endy;
    >
    >
    > while (startx != endxp){
    > std::cout << " *startx " << *startx << std::endl;
    > while (starty != endyp) {
    > std::cout << " *starty " << *starty << std::endl; starty++;
    > }
    >
    >
    > startx++;
    > }
    >
    > but when I run it I get:
    >
    > *startx 11
    > *starty 1
    > *starty 2
    > *starty 3
    > *starty 9
    > *startx 22
    > *startx 33
    > *startx 44
    > *startx 55
    > *startx 77
    > *startx 88
    > *startx 99
    >
    > Any ideas on why the inner while only gets executed once?


    You need to reset the value of starty, either before or after the inner
    loop.

    -Howard
    Howard, Apr 26, 2007
    #2
    1. Advertising

  3. desktop

    Salt_Peter Guest

    On Apr 26, 4:48 pm, desktop <> wrote:
    > I have this nested while:
    >
    > int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99};
    > int* startx = numsx;
    > int endx = sizeof(numsx) / sizeof(numsx[0]);
    > int* endxp = numsx + endx;
    >
    > int numsy[] = { 1, 2, 3, 9};
    > int* starty = numsy; // move this line
    > int endy = sizeof(numsy) / sizeof(numsy[0]);
    > int* endyp = numsy + endy;
    >
    > while (startx != endxp){
    > std::cout << " *startx " << *startx << std::endl;
    >


    int* starty = numsy;

    > while (starty != endyp) {
    > std::cout << " *starty " << *starty << std::endl;
    > starty++;
    > }
    >
    > startx++;
    >
    > }
    >
    > but when I run it I get:
    >
    > *startx 11
    > *starty 1
    > *starty 2
    > *starty 3
    > *starty 9
    > *startx 22
    > *startx 33
    > *startx 44
    > *startx 55
    > *startx 77
    > *startx 88
    > *startx 99
    >
    > Any ideas on why the inner while only gets executed once?


    What is pointer starty set to at the beginning of the second pass of
    the inner_loop?
    Why should the inner while-loop repeat since starty == endyp following
    the first run?
    Is that not what you told the program to do?
    Salt_Peter, Apr 27, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Perry Jr
    Replies:
    2
    Views:
    4,124
    Russ Perry Jr
    Aug 20, 2004
  2. Mudassar
    Replies:
    0
    Views:
    365
    Mudassar
    Jul 10, 2006
  3. Mudassar
    Replies:
    0
    Views:
    397
    Mudassar
    Jul 10, 2006
  4. invni
    Replies:
    36
    Views:
    876
    Robert Maas, see http://tinyurl.com/uh3t
    Jul 27, 2005
  5. Replies:
    3
    Views:
    466
    Victor Bazarov
    Dec 7, 2007
Loading...

Share This Page