Problem with nested while

D

desktop

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?
 
H

Howard

desktop said:
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
 
S

Salt_Peter

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top