for looping and if

Hi! I'm new to this forum and I would like to ask some help.
I am having a problem with this program:

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";
   }
}

Program is supposed to be:
Input = 5
Output

....9....
....8....
....7....
....6....
123456789
....4....
....3....
....2....
....1....


But it ends up being:
Input = 5

....9....
....8....
....7....
....6....
123456789....5....
....4....
....3....
....2....
....1....
]

EDIT: I've change the Code format for better viewing
Last edited on
You forgot a new line on line 10. EDIT: It's more than that, see AbstractionAnon's post below.

Also, you should use a better indentation style for your code. Additionally consider always using curly braces even when you only have one statement inside.
Last edited on
Line 10: You want an else statement to exclude printing the dotted line if if z and a match.
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
123456789
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.
Last edited on
sorry for causing so much trouble for you guys.. I usually use curly braces. it's just that i want to try to see if there's any difference with and without braces. And for the line 10 thing? what kind of else statement should i put? I've been thinking of what exception I could make to stop "z" from printing dotted lines when "z==a"..
I've made few fixes. The comment on your code is to make it easy to see which parts were changed.

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
#include <iostream>
using namespace std;
int main()
{
    int a = 0;
    cin >> a;
    //for(int i=1;i<=9;i++)
    for(int i = 9; i > 0; --i)
    {
        //if(z == a)
        if(i == a)
        {
            //cout << "123456789";
            cout << "123456789" << std::endl;
        }
        /*
         * Else will only be executed as soon as the above condition is false.
         * */
        else
        {
            for(int j=1;j<=9;j++)
            {
                if (j==a)
                {
                    //cout << z--;
                    cout << i;
                }
                else
                {
                    cout << ".";
                }
            }
            cout << std::endl;
        }
    }
}

Last edited on
Topic archived. No new replies allowed.