After line 10, you proceed to doing your inner for loop, regardless of whether or not
z == a
. So when z is 5, line 10 prints out
and then goes on to loop and print the dots.
You should use an
else
block for the code you want to execute when
z == a
is not true.
I
strongly recommend you use a consistent, sensible indentation style in your code. That will make it much easier for you to see the flow of control through your code at a glance, and see where there might be a mistake in the logic.
Also, I'd advise using curly braces even when a block contains only a single line. That helps guard against introducing errors when modifying your code.
If that were my code, It would be formatted like this:
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
|
#include <iostream>
using namespace std;
int main()
{
int a,b,c,d,z;
z=9;
cin >> a;
for(int i=1;i<=9;i++)
{
if(z==a)
{
cout << "123456789";
}
for(int j=1;j<=9;j++)
{
if (j==a)
{
cout << z--;
}
else
{
cout << ".";
}
}
cout << "\n";
}
}
|
Now it's much easier to see at a glance how the logic of it works - and why you're seeing the behaviour you're seeing.