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.
As I stated in my comments to your earlier post, you may want to try printing without the
for
loop:Probably, an easier solution for you is to declare the text as a C-Style string:
You can then print lines before:
Or print lines after:
Your animation may look something like:
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;
withstd::cout.write(ok_lines, sizeof(ok_lines) - 1);
. The-1
prevents the nul terminator character from being written to the console.