Nested Loops, Count up then down...
Dec 17, 2013 at 6:40pm UTC
I was wondering if anyone could help me point out whats wrong with my code. I want to use a nested for loop and have the loop count up like ive done. Only problem is I want it to count like so...
"1
12
123
1234
12345"
Instead im getting
"1
23
456
78910
1112131415"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int row, col;
int size;
int mych;
cout << "Size of Shapes (>0) " ;
cin >> size;
while (size<0)
{
cout << "\nSize of Shapes (>0) " ;
cin >> size;
}
cout << "Characters to use: " ;
cin >> mych;
for (row = 1; row <= size; row++)
{
cout << "\n" << setw(8);
for (col = 1; col <=row; col++)
{
cout << mych;
mych++;
}
}
cout << "\n\n" ;
system ("pause" );
return 0;
}
Last edited on Dec 17, 2013 at 8:52pm UTC
Dec 17, 2013 at 6:53pm UTC
you need to reset
mych
in your outer
for
loop.
1 2 3 4 5 6 7 8 9 10 11 12
char printch;
for (row = 1; row <= size; row++)
{
printch = mych;
cout << "\n" << setw(8);
for (col = 1; col <=row; col++)
{
cout << printch;
printch++;
}
}
Dec 17, 2013 at 9:13pm UTC
Why thank you very much!
What if I wanted to count down? What would I do then?
"12345
1234
123
12
1"
Dec 18, 2013 at 12:47am UTC
You can reverse your outer for loop to count down from a larger number instead of counting up from a smaller number:
for (row = size; row > 0; --row)
Topic archived. No new replies allowed.