@gentleguy
Stop trolling.
@carlwryker
Maybe use for loops since they are better for what you're using right now.
I'll address your problem now:
- Your while loops
ARE working correctly but I'll explain why it seems the outer loop executes only once.
====================================================================
Here is what happens:
1. Your program starts executing outer loop and starts executing the loop inside.
2. Once tempId2 is equal to maxId because of the increment statement ++tempId2 it goes out of this inner loop.
3. Then it executes ++tempId;.
4. And NOW it executes outer loop over and over again
BUT it does not execute the inner loop because now the inner loop evaluates to false (tempId2 != maxId).
Conclusion: So basically it execute outer loop, goes into inner loop and repeats it 5 times, and
THEN repeats the outer loop 5 times but it is meaningless because nothing is executed in the outer loop after that. Therefore the console prints 5 lines and then prints none after that.
How I would write your code (this will print 25 lines as you want):
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
int main()
{
unsigned int maxId = 6;
for (unsigned int tempId {1}; tempId != maxId; ++tempId)
{
for (unsigned int tempId2 {1}; tempId2 != maxId; ++tempId2)
{
std::cout << tempId << " , " << tempId2 << std::endl;
}
}
std::cin.ignore();
}
|
The reason the above works is because the
ENTIRE inner for loop is executed meaning
unsigned int tempId2 {1};
is executed every time the outer for loop repeats.
If you want to use your while loops you can put
tempId2 = 1
right before or after
++tempId;
std::cin.ignore();
pauses.