#include <iostream>
usingnamespace std;
int main ()
{ int y = 0;
int x = 0;
int someArray[2][3] = {{2,3,4},{5,6,9}};
while(x < 2)
{ y = 0; // y needs to be reset on each iteration
while ( y < 3)
{ cout << someArray[x][y] << " ";
y++;
}
x++;
cout << "\n" << endl;
}
system ("pause");
return 0;
}
Thanks man, I knew it was simple and obvious but I kinda assumed every time a loop finishes completely it takes the value from the local scope where it is defined which in this case is zero. So guess I was wrong.
Maybe it would be smart to write a small function to check that, like this:
#include <iostream>
usingnamespace std;
int main () {
int y = 0;
int x = 0;
int someArray[2][3] = {{2,3,4},{5,6,9}}; // [1st] is the row, [2nd] is the column
while(x < 2) {
y = 0;
while ( y < 3){
cout << someArray[x][y] << " ";
y++;
}
x++;
cout << "Value of Y after while loop ends: " << y << endl;
cout << "\n" << endl;
}
return 0;
}
No, not that, luckily I'm familiar with for loops and how they function unlike the while loops.
I explain stuff terribly, when I said local scope I actually meant the int y = 0; beneath int main (). And why would I think about for loops? I couldn't possibly be that dumb to confuse it with while loops, where else does the while loop have initialization of it's variable then on the start of the int main? That's what I was thinking about. Sorry for ranting here, but this really grinds my gears.