Please enter the maximum field width: 9
Enter the microseconds to pause between asterisks: 50000
*
.*
..*
...*
....*
.....*
......* (THE PERIODS ARE EMPTY SPACE)
.......*
........*
.......*
......*
.....*
....*
...*
..*
.*
*
.*
..*
...*
....*
.....*
......*
.......*
........*
.......*
......* (THE PERIODS ARE EMPTY SPACE)
.....*
....*
...*
..*
.*
*
.*
..*
...*
....*
.....*
......*
.......*
........*
.......*
......*
.....*
....* (THE PERIODS ARE EMPTY SPACE)
...*
..*
.*
*
^C
however I cannot get the tooth design correctly. I know that to get it to come back I need to subtract the users field width by 1 but then I cant get it to go back.... any help and explanation would be appreciated.
#include <iostream>
//allows you to call the standard library to suspend your program for
// microseconds (using usleep()).
#include <unistd.h>
// allows you to set widths
#include <iomanip>
usingnamespace std;
//=================================MAIN========================================
//=============================================================================
//=============================================================================
int main()
{ //need counter = 2 because setw(0) = setw(1) = setw(2). they look the same.
int counter = 1;
int fieldWidth;
//outputSuspender is in microseconds so it take 1 million to make 1 second.
int outputSuspender;
cout << "Please enter the maximum field width: ";
cin >> fieldWidth;
cout << "Enter the microseconds to pause between asterisks: ";
cin >> outputSuspender;
//Make infinity loop to always output the asterisk design
while( 1 )// loop forever. User will terminate app.
{
while (counter <= fieldWidth)
{
cout << setw(counter) << "*" << endl;
++counter;
usleep(outputSuspender);
}
while (counter > fieldWidth)
{
cout << setw(fieldWidth - 1) << "*" << endl;
--fieldWidth;
usleep(outputSuspender);
}
}// End Of Main Function
return 0;
}
Why not just keep using counter as you have been in the previous loop? Keep decrementing counter until it hits zero, rather than messing with fieldWidth.
With the way you are trying to do it, you will need a directional indicator variable. For example,
bool dir_increasing = true;
Then, inside the first while loop, use an if statement to check which direction you are going in before adjusting your fieldWidth for the next iteration.