-1

My app's work is after getting text value from user the app print it with pattern horizontally, but it doesn't show any thing.

for make question shorter i just put 2 patterns of 26:

int main()
{

printf("Please inter a text:");
string input;
cin >> input;

char ptn[2][7][13] = {{
        {' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' ', 'A', ' ', 'A', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', 'A', 'A', 'A', 'A', 'A', 'A', 'A', ' ', ' ', ' '},
        {' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' '},
        {' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' '},
        {'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A'}},

        {{'B', 'B', 'B', 'B', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', 'B', 'B', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', 'B', 'B', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}}};

for (int y = 0; y < 7; y++) {
    for (int i = 0; input[i] != '\0'; i++) {
           cout << ptn[input[i]][y];
    }

    cout << "\n";
}
return 0;
}

output should be:

      A       BBBB                                                                                                              
     A A      B   B                                                                                                             
    A   A     B   B                                                                                                             
   AAAAAAA    BBBB                                                                                                              
  A       A   B   B                                                                                                             
 A         A  B   B                                                                                                             
A           A BBBB 
Fazel
  • 23
  • 6

3 Answers3

1
  • ptn[2][7][13] is difficult to understand. Break the definition in subtypes. A glyph_set is made of 26 glyphs. A glyph is made of 7 rows. A row is made of 5 columns (plus 1 for null terminator):

    enum
    {
      col_count = 5 + 1,
      row_count = 7,
      glyph_count = 26
    };

    typedef const char row_t[ col_count ];
    typedef const row_t glyph_t[ row_count ];
    typedef const glyph_t glyph_set_t[ glyph_count ];
  • A line of glyphs is made of 7 rows and 5 columns multiplied by the number of characters the line is made of. If your text is "AB" the line will be made of 7 rows and 10 columns (plus a space between letters).

Your program (demo):

#include <iostream>
#include <string>
#include <cctype>

enum
{
  col_count = 5 + 1,
  row_count = 7,
  glyph_count = 26
};

typedef const char row_t[ col_count ];
typedef const row_t glyph_t[ row_count ];
typedef const glyph_t glyph_set_t[ glyph_count ];
typedef std::string line_t[ row_count ];

glyph_set_t gs
{
  {
    {"  A  "},
    {" A A "},
    {"A   A"},
    {"A   A"},
    {"AAAAA"},
    {"A   A"},
    {"A   A"},
  },

  {
    {"BBBB "},
    {"B   B"},
    {"B   B"},
    {"BBBB "},
    {"B   B"},
    {"B   B"},
    {"BBBB "},
  },
  //...
};


int main()
{
  const char* s = "AB";

  for( int r = 0; r < row_count; ++r )
  {
    for( const char* p = s; *p; ++p )
    {
      int set_idx = std::toupper( *p ) - 'A';
      // this...
      glyph_t& g = gs[ set_idx ];
      std::cout << g[ r ] << ' ';
      // ...or this (whichever is easier for you)
      // std::cout << gs[ set_idx ][ r ] << ' ';
    }
    std::cout << std::endl;
  }
  return 0;
}
zdf
  • 4,382
  • 3
  • 18
  • 29
  • this app should only work with array and for loop not any more. and 'A' must have 13 col not 5 – Fazel Oct 26 '18 at 11:56
  • _"not any more"_ - you should specify the restriction in your question. _" 'A' must have 13 col not 5"_ you can easily change `col_count` & `row_count`. – zdf Oct 26 '18 at 12:05
  • @Fazel Jus to be clear: you are not allowed to use functions, or you are not allowed to use `while`? – zdf Oct 26 '18 at 12:10
  • "you are not allowed to use functions, or you are not allowed to use while?" Yeah! – Fazel Oct 26 '18 at 12:11
0

Firstly, you’re trying to iterate an std::string in a way that will just not work:

for (int i = 0; input[i] != '\0'; i++)

With no null-termination, this loop will run wild. Either check i against input.size(), or simply:

for (auto c : input)

The above may not be true.


Secondly, your character-to-index mapping is off: If I enter an A, then ptn[input[i]] is ptn['A'], which in turn is ptn[0x41], which is out-of-range of ptn.

An A should map to the index 0, a B to 1, etc. So either do the right ‘ASCII math’, or use something other than an array, e.g. std::map<std::vector<std::string> > and ‘manually’ map each character.


Thirdly, and perhaps not as importantly for now, you’re not checking whether the input consists of only letters that you have in ptn.

Since it’s at least possible to enter such letters you should either skip them or fail with some kind of error message for the user.

Biffen
  • 6,249
  • 6
  • 28
  • 36
0

Try passing your input as inside operator[]. A check for is alphabet can ensure we are not passing out of scope 26 in your case.

int main()
{
    const int PATTERN_MAX_LENGHT = 7;
    const int PATTERN_MAX_HEIGHT = 13;
    const int MAX_PATTERNS = 1;

    const char pattern [MAX_PATTERNS][PATTERN_MAX_LENGHT][PATTERN_MAX_HEIGHT] = 
    {
        {
            {' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', ' ', 'A', ' ', 'A', ' ', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' '},
            {' ', ' ', ' ', 'A', 'A', 'A', 'A', 'A', 'A', 'A', ' ', ' ', ' '},
            {' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' '},
            {' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' '},
            {'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A'}
        }
    };


    char chInput;

    cin>>chInput;
    if( ! isalpha ( chInput ) )
    {
        //Not an Alphabet
        return false;
    }
    chInput = tolower( chInput );

    int index = chInput - 'a';

    //Print in loop
    for(int i =0;i < PATTERN_MAX_LENGHT  ; i++ )
    {
        for(int j=0;j < PATTERN_MAX_HEIGHT ; j++)
        {
            cout<<pattern[index][i][j];
        }
        cout<<endl;
    }

    return 0;
}
Kiran Thilak
  • 443
  • 5
  • 15