-1

I want to change my character drawing to other characters other than #. How do I change it without having to make a new board for the new character? other than that i also want to compile the program so it can be horizontal I am planning to use 'if else' statement but not sure whether or not it can be done.

#include <iostream>
#include <cstdlib>      
#include <string>
using namespace std;

void display( string displayBoard[][ 7 ], int row);
void clearScren();
void delay();

int main()
{
  string letter;
  string displayBoard[ 7 ][ 7 ];

  cout << "Enter a letter or number:\n";
  getline(cin, letter);     

  for (int i = 0; i < letter.length(); ++i) 
  {
        switch (letter[i])
        {
            case 'c' :
            case 'C' :
                   for (int a = 0; a < 1; ++a)
                   {
                    displayBoard[ 0 ][ a ] = "  #### ";
                    displayBoard[ 1 ][ a ] = " #    #";
                    displayBoard[ 2 ][ a ] = " #     ";
                    displayBoard[ 3 ][ a ] = " #     ";
                    displayBoard[ 4 ][ a ] = " #     ";
                    displayBoard[ 5 ][ a ] = " #    #";
                    displayBoard[ 6 ][ a ] = "  #### ";
                    display( displayBoard, 7);
                   }
                break;

            case 'd' :
            case 'D' :
                    for (int a = 0; a < 1; ++a)
                    {
                     displayBoard[ 0 ][ a ] = " ##### ";
                     displayBoard[ 1 ][ a ] = " #    #";
                     displayBoard[ 2 ][ a ] = " #    #";
                     displayBoard[ 3 ][ a ] = " #    #";
                     displayBoard[ 4 ][ a ] = " #    #";
                     displayBoard[ 5 ][ a ] = " #    #";
                     displayBoard[ 6 ][ a ] = " ##### ";
                     display( displayBoard, 7);
                    }
                break;
      }
  }
  return 0;
}

void display( string displayBoard[][ 7 ], int row ) 
{
   for( int i = 0; i < row; ++i )
   {
      for( int j = 0; j < 7; ++j )
      {
         cout << displayBoard[ i ][ j ];
      }
      cout << endl;
   }
   delay();
   clearScren();
}

void delay()    
{
   for( int i = 0; i < 300000000; ++i )
   { }
}

void clearScren()
{
   system( "cls" );
}
aron13214
  • 27
  • 3
  • it is not clear why `displayBoard` is a 2-d array of strings. You dont need 3 dimensions for a 2-d board. If you had a 2d array of characters then transposing the board would be just swapping the order of the loops for `i` and `j` – 463035818_is_not_an_ai Sep 23 '19 at 14:57
  • 1
    also the "use different characters" part of the question is unclear. You want to replace `#` with some other character? You could do so by replacing `#` with other characters in the code – 463035818_is_not_an_ai Sep 23 '19 at 14:58
  • 2
    `for (int a = 0; a < 1; ++a)` is a very roundabout way of doing something exactly once. You're only using the first element of each `displayBoard[k]`. – molbdnilo Sep 23 '19 at 14:58
  • @formerlyknownas_463035818 Yes.I intend to let user to choose which character to replace the #. – aron13214 Sep 23 '19 at 15:02
  • is there a way to replace # to other character if i need to let user choose other character to replace the character #? – aron13214 Sep 23 '19 at 15:12

3 Answers3

0

"I want to change my character drawing to other characters other than #." You can use std::replace to replace each '#' by another char.

"other than that i also want to compile the program so it can be horizontal" You have to build each row before you print it to align the letters horizontally. You can use std::string::operator+= to append each row of the letter to the display board:

"I am planning to use 'if else' statement but not sure whether or not it can be done." It can be done but I think switch / case makes more sense here.

#include <array>
#include <iostream>
#include <string>

void display(std::array<std::string, 7> &displayBoard, char sym);
// void display(std::array<std::string, 7> displayBoard, char sym);

int main() {
  std::string letter;
  std::array<std::string, 7> displayBoard;

  std::cout << "Enter a letter or number:\n";
  std::getline(std::cin, letter);

  char sym;
  std::cin >> sym;

  for (std::size_t i = 0; i < letter.length(); ++i) {
    if (letter[i] == 'c' or letter[i] == 'C') {
      displayBoard[0] += "  #### ";
      displayBoard[1] += " #    #";
      displayBoard[2] += " #     ";
      displayBoard[3] += " #     ";
      displayBoard[4] += " #     ";
      displayBoard[5] += " #    #";
      displayBoard[6] += "  #### ";
    } else if (letter[i] == 'd' or letter[i] == 'D') {
      displayBoard[0] += " ##### ";
      displayBoard[1] += " #    #";
      displayBoard[2] += " #    #";
      displayBoard[3] += " #    #";
      displayBoard[4] += " #    #";
      displayBoard[5] += " #    #";
      displayBoard[6] += " ##### ";
    }
  }
  display(displayBoard, sym);
  return 0;
}

void display(std::array<std::string, 7> &displayBoard, char sym)  {
// void display(std::array<std::string, 7> displayBoard, char sym)  {
// use a copy instead of the reference to not change the original
   for(std::size_t i = 0; i < displayBoard.size(); ++i ) {
     std::replace(std::begin(displayBoard[i]), std::end(displayBoard[i]), '#', sym);
     std::cout << displayBoard[i];
     std::cout << '\n';
   }
}

Input:

CDD
a

Output:

  aaaa  aaaaa  aaaaa 
 a    a a    a a    a
 a      a    a a    a
 a      a    a a    a
 a      a    a a    a
 a    a a    a a    a
  aaaa  aaaaa  aaaaa 

In addition I removed the third dimension of your display board because it wasn't used. I removed row in display because with std::array the size is stored in the variable and it's not necessary to pass the size with the array. I removed using namespace std; because Should I use using namespace std in my code? and Why is “using namespace std;” considered bad practice?. I changed the C-style arrays to std::array because ES.27: Use std::array or stack_array for arrays on the stack and SL.con.1: Prefer using STL array or vector instead of a C array

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • thanks for the reply.I was wondering if there is any way to allow users to select which character they want.For example, can i set H = '#' , Q = '?' and D = '.' so that the users are able to choose one of them? – aron13214 Sep 23 '19 at 16:18
  • @arontan Of course, you can use `std::cin`. I changed my answer. – Thomas Sablik Sep 23 '19 at 16:31
0

As other pointed out, your code has a bunch of problems, but for a small program that's fine. If you want to improve your code I will try to help you a bit at the end. However, to only answer your question, C++ has no built-in function to replace all characters in a string. However you can build your own function relatively easily. I assume you don't know about containers yet, so a possible implementation of such a function would be :

void replaceInStr(string& input, char oldChar, char newChar) {
  for (int i = 0; i < input.length(); ++i) {
    if (input[i] == oldchar) {
      input[i] = newChar;
    }
  }
}

As I mentionned, that code can be a lot cleaner if you are willing to use containers:

void replaceInStr(string& input, char oldChar, char newChar) {
  replace(input.begin(), input.end(), oldChar, newChar);
}

This function works on strings because strings are C++ containers and can be iterated through.


Now if you want a few advices for your code, here is a cleaned up version:

#include <iostream>
#include <cstdlib>      
#include <string>
using namespace std;

void display(string displayBoard[7], int row);
void clearScren();
void delay();

int main()
{
    string letter;
    string displayBoard[7];

    cout << "Enter a letter or number:\n";
    getline(cin, letter);

    for (int i = 0; i < letter.length(); ++i)
    {
        switch (letter[i])
        {
        case 'c':
        case 'C':
            displayBoard[0] = "  #### ";
            displayBoard[1] = " #    #";
            displayBoard[2] = " #     ";
            displayBoard[3] = " #     ";
            displayBoard[4] = " #     ";
            displayBoard[5] = " #    #";
            displayBoard[6] = "  #### ";
            display(displayBoard, 7);
        break;

        case 'd':
        case 'D':
            displayBoard[0] = " ##### ";
            displayBoard[1] = " #    #";
            displayBoard[2] = " #    #";
            displayBoard[3] = " #    #";
            displayBoard[4] = " #    #";
            displayBoard[5] = " #    #";
            displayBoard[6] = " ##### ";
            display(displayBoard, 7);
            break;
        }
    }
    return 0;
}

void display(string displayBoard[7], int row)
{
    for (int i = 0; i < row; ++i)
    {
        cout << displayBoard[i];
        cout << endl;
    }
    delay();
    clearScren();
}

void delay()
{
    for (int i = 0; i < 300000000; ++i)
    {
    }
}

void clearScren()
{
    system("cls");
}
0

I highly recommend moving away from the switch statement to using arrays.

Ancient devices, such as dot matrix printers, raster displays, laser printers and ink jet printers would use bitmap fonts. The font would consist of an array of bitmaps, one for every printable character.

These devices would have a "raster" buffer. The formatter function would copy bitmaps (one for each character in the output line), into a "raster" buffer. The raster function would print the top rows of every bitmap, then the second rows, and so on until all the rows of the bitmaps were printed.

This algorithm reduces the need for a switch statement for each bitmap. Also, it adds more genericity so that there is less coupling between the bitmaps and the letters (also allows for custom bitmaps to be placed into the buffer). This technique converts to being data driven; you can change the behavior by changing the data and not having to change the executable code. In your code, to add another letter or bitmap, one has to add another case statement.

Edit 1: Example code fragments

typedef std::array<std::array<char, 5>, 7> BitMap; // 5 columns x 7 rows
Bitmap Letter_C = {{
  {"#####"},
  {"#    "},
  {"#    "},
  {"#    "},
  {"#    "},
  {"#    "},
  {"#####"},
} };

void Print_Single_Bitmap(const Bitmap& b, std::ostream& output)
{
    for (int row = 0; row < 7; ++row)
    {
        for (int column = 0; column < 5; ++column)
        {
            output << b[row][column];
        }
        output << "\n";
    }
}

Note: above code is not tested; row and column may need to be switched.

typedef std::vector<Bitmap> Bitmap_Buffer;
void Print_Bitmap_Line(const Bitmap_Buffer& buffer, std::ostream& output)
{
  const unsigned int quantity = buffer.size();
  for (int row = 0; row < 7; ++row)
  {
      for (unsigned int bitmap_index = 0; bitmap_index < quantity; ++bitmap_index)
      {
          for (int column = 0; column < 5; ++column)
          {
               output << buffer[bitmap_index][row][column];
          }
          output << "   ";
      }
      output << "\n";
   }
}

The intent of the above code is to print each row of the bitmap container, then a newline. The columns are printed, then a spacing between bitmaps.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154