1

First and foremost, I'm a beginner, trying to figure out how to print the letters 'OK' moving up and down using asterisk. The goal is to start at an offset position and have them increment and decrements x amount of times. (I'm not on this part yet). But first I'm stuck on why the 'K' isn't printing out the way I want it to. Can anyone offer assistance?

I've tried setting up a for loop that handles the printing of each character. I have attempted to make counters so that when the characters get printed they seem to be offset, because I know that if they both start at line 0 then they will print side by side. Below is some of the code I have worked out so far. The 'O' print but the 'K' is printing weirdly.

int main() {
char userInputCharacter;   // User input for some character
int  userInputNumber;      // User input for some numerical value

cout << endl
     << "Choose from among the following options: \n"

     << "   2. Display OK as an animation \n"

     << "Your choice -> ";
cin >> userInputCharacter;
cout << endl;


// Display OK as an animation
if( userInputCharacter == '2') {
    cout << "How many sets of letters do you want to display? -> ";
    cin >> userInputNumber;

    for( int setsOfLetters = 0; setsOfLetters < userInputNumber; setsOfLetters++) {

        // Display some number of blank lines.  This starts as a large number the first time, then
        // gets smaller each subsequent time, moving the ENTIRE set of letters vertically.
        for( int numberOfBlankLines = userInputNumber; numberOfBlankLines > setsOfLetters; numberOfBlankLines--) {
            cout << endl;
        }

        // Display one set of letters, going through and printing one "slice" of each letter at a time.

        int i = 0;
        int j  = 4;
        for( int i=0; i<8; i++) {
            if(      i==0)    cout << "       ";
            else if( i==1) cout    << "       ";
            else if( i==2) cout    << "  **   ";
            else if( i==3) cout    << " *  *  ";
            else if( i==4) cout    << "*    * ";
            else if( i==5) cout    << "*    * ";
            else if( i==6) cout    << " *  *  ";
            else if( i==7) cout    << "  **   ";


            if(      j ==0) cout << "      ";
            else if( j ==1) cout<< "       ";
            else if( j ==2) cout << "*  * ";
            else if( j ==3) cout << "* *  ";
            else if( j ==4) cout << "**   ";
            else if( j ==5) cout << "* *  ";
            else if( j ==6) cout << "*  * ";
            else if( j ==7) cout << "       ";

            cout << endl;


        }//end for( int i...)

        // Clear the screen after the letters are displayed.
        this_thread::sleep_for(chrono::milliseconds( 185));    // Sleep for 185 milliseconds
        system( "clear");   // Clear the screen.  Comment out this line if you don't want them erased.

    }//end for( int setsOfLetters...

}//end else if( userInputCharacter == '2' ...

return 0;
}//end main()

It would be beneficial to me if we can suggest using for loops and if and else statements to solve this. I have not yet covered functions and strings nor arrays and I don't want to jump ahead and get even more confused. Thank you all in advance.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Elchavo18
  • 60
  • 9
  • 1
    You can avoid those all "if/else" and just use "cout". But be aware that `cout << "something"; cout << "other"` will display "somethingother". Add `\n` or `<< endl` to display in a new line. – Ripi2 Sep 05 '19 at 15:58
  • that would make the OK letters display on top of each other wouldn't it? i want them to stay on the same line just offset – Elchavo18 Sep 05 '19 at 16:02
  • 1
    Then join every two proper slices in an only `cout << "..." << endl`, – Ripi2 Sep 05 '19 at 16:03
  • 1
    For your current code, `j` is useless and unchanged. – Ripi2 Sep 05 '19 at 16:05
  • How would i make j un-useless and make it work so i get my objective. I wanted j as a counter to help offset the letters. Thats why i initialized it to start printing at 4. – Elchavo18 Sep 05 '19 at 16:09
  • `i ` and `j` are both "slice selectors". Because you want always the same number of slices then just replace `j ` with `i` (i.e. repeat same if/else's) You need to build a `std::string` with the number of blank spaces between the two sides of the same slice (i.e. the offset between letters) and "cout" it between "O" side and "K" side – Ripi2 Sep 05 '19 at 16:14
  • could you help out with that build? – Elchavo18 Sep 05 '19 at 16:17
  • `std::string s(4, 'x');` means `s` is "xxxx" – Ripi2 Sep 05 '19 at 16:19
  • where would i place it in my code? – Elchavo18 Sep 05 '19 at 16:20
  • Please, use a good C++ book. This is my last tip: i=j=4 slice: `cout << "* * " << s << "** ";` assuming you keep similar your current code. – Ripi2 Sep 05 '19 at 16:23

1 Answers1

0

As I stated in my comments to your earlier post, you may want to try printing without the for loop:

std::cout << "  **   *  * \n";
std::cout << " *  *  * *  \n";
std::cout << "*    * **   \n";
std::cout << "*    * * *  \n";
std::cout << " *  *  *  * \n";
std::cout << "  **   *   *\n";

Probably, an easier solution for you is to declare the text as a C-Style string:

static const char ok_lines[] =
{
    "  **   *  * \n"
    " *  *  * *  \n"
    "*    * **   \n"
    "*    * * *  \n"
    " *  *  *  * \n"
    "  **   *   *\n"
};

You can then print lines before:

std::cout << "\n\n";
std::cout << ok_line;

Or print lines after:

std::cout << ok_line;
std::cout << "\n\n";

Your animation may look something like:

static const unsigned int MAX_CYCLES = 10U;
for (unsigned int i = 0; i < MAX_CYCLES; ++i)
{
   Clear_The_Screen();
   if ((i % 2) == 0)
   {
      std::cout << ok_lines;
      std::cout << "\n\n";
   }
   else
   {
      std::cout << "\n\n";
      std::cout << ok_lines;
   }
   Delay();  // You'll need to write or research this one.
}

Reminder: Simple designs produce simple code. Simple code is easier to debug and has fewer injected defects.

Note: If you want more efficient output, replace std::cout << ok_lines; with std::cout.write(ok_lines, sizeof(ok_lines) - 1);. The -1 prevents the nul terminator character from being written to the console.

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