Variable not functioning properly

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
#include <iostream>

using namespace std;

int main()
{
	int n, x, blanks, rows, irow, crow=1, a, y, z;
	cout<<"Enter a positive odd number: ";
	cin>>n;
	while(n%2==0 || n<0)
	{
		cout<<"Enter a positive odd number. Try again: ";
		cin>>n;
	}
	irow=n;
	a=n;
	for (x=n;x>=1;x=x-2)
	{
		for(blanks=1;blanks<=crow-1;blanks++)
		{
			cout<<" ";
		}
		for (irow=x;irow<=(2*x)-1;irow++)
		{
			cout<<a%10;
			a--;
		}
		cout<<"\n";
		crow++;
		a++;
	}
	return 0;
}

For some reason, I get the cout of

321
1

Because of the a++, shouldn't it be

321
2

?
The second example is what the cout needs to be. How do I fix it?
In your code:
1
2
3
4
cout<<a%10; //This outputs 1 in the end of 321. a == 1
a--; //Now a == 0 
/*...*/
a++; //Now a == 1 
Topic archived. No new replies allowed.