0

I have been recently solving a coding question that requires an user to input a 3xN matrix that consists of only '.', '*', and '#'. Then there are some patterns mentioned in the question that is taken care of in the code below, but there's a problem. The problem is I have if-else statements inside a for loop. But when an if block matches it does prints out the requires character but the for loop terminates. I want the for loop to continue till 'N'.

    #include<bits/stdc++.h>
    #include<string.h>
    using namespace std;

    int main(){
     int N;
     cin >> N;
     if(N>=3 && N<=100000){
        char matrix[3][N];
        string p;
        for(int i=0;i<3;i++)
            for(int j=0;j<N;j++)
                cin >> matrix[i][j];
                
        for(int i=0; i<N; i+=2){
            if(matrix[0][i] == '.')
                continue;
            else if(matrix[0][i] == '#'){
                cout << '#';
                continue;
            } else if(matrix[0][i] == '*'){
                //E
                if((i+2)<N && matrix[0][i+1]=='*' && matrix[0][i+2]=='*' && matrix[1][i]=='*' && 
               matrix[1][i+1]=='*' && matrix[1][i+2]=='*' && matrix[2][i]=='*' && matrix[2][i+1]=='*' && 
               matrix[2][i+2]=='*'){
                    p +='E';
                    continue;
                //A
                } else if((i+2)<N && matrix[0][i+1]=='.' && matrix[1][i]=='*' && matrix[1][i+1]=='*' && 
               matrix[1][i+2]=='*' && matrix[2][i]=='*' && matrix[2][i+1]=='.' && matrix[2][i+2]=='*'){
                    p += 'A';
                    continue;
                //I
                } else if((i+2)<N && matrix[0][i+1]=='*' && matrix[0][i+2]=='*' && matrix[1][i]=='.' && 
                   matrix[1][i+1]=='*' && matrix[1][i+2]=='.' && matrix[2][i]=='*' && matrix[2][i+1]=='*' 
               && matrix[2][i+2]=='*'){                 
                    p += 'I';
                    continue;
                //O
                } else if((i+2)<N && matrix[0][i+1]=='*' && matrix[0][i+2]=='*' && matrix[1][i]=='*' && 
               matrix[1][i+1]=='.' && matrix[1][i+2]=='*' && matrix[2][i]=='*' && matrix[2][i+1]=='*' && 
               matrix[2][i+2]=='*'){
                    p += 'O';
                    continue;
                //U
                } else if((i+2)<N && matrix[2][i]=='*' && matrix[2][i+1]=='*' && matrix[2][i+2]=='*' && 
                matrix[0][i+1]=='.' && matrix[0][i+2]=='*' && matrix[1][i]=='*' && matrix[1][i+1]=='.' && 
                matrix[1][i+2]=='*'){
                    p += 'U';
                    continue;
                } else
                    continue;
            } else {
                cout << "Invalid Input";
                continue;
            }
        }
        cout << p;
    } else {
        cout << "Invalid Input";
    }
}
Input: * * * #
       * * * #
       * * * #
Output : E

What should it print (Correct output): E#
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Due to `i+=2` in the `for` loop, `i` will be `0, 2` and the column with `#` (`i = 3`) won't be visited. – MikeCAT Aug 09 '20 at 17:26
  • 7
    The shown code demonstrates multiple undesirable programming practices, like non-standard header files, non-standard variable length arrays, spaghetti-like if/else trees, and others. This is typical when one focuses their attention on pointless online coding/hacking/competition sites, that offer no valuable C++ skills to learn from. Someone who actually wants to improve their C++ skills will find that the only way to do so is [with a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Aug 09 '20 at 17:29
  • This doesn't address the question, but none of the `continue;` statements inside the loop is needed. You can remove all of them, except for the first, where you can replace `continue;` with `;`. That will save readers of your code from hunting for the one that's different, which doesn't exist. – Pete Becker Aug 09 '20 at 19:16

1 Answers1

2

cout << p; is after the loop. It outputs E, but by then you're already out of the loop and it's too late to output #.

You're essentially comparing the contents of matrices in these nested ifs. You should make that a separate function, with the comparison elements also arranged in a matrix. It would make this whole thing more easy to digest.

Geno C
  • 1,401
  • 3
  • 11
  • 26