ZigZag Design Help

My program is suppose to look like this:

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #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>
using namespace 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;
}
Last edited on
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.
Topic archived. No new replies allowed.