Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

c++ zigzag pattern setw

Hello, i'm stuck ones again.... the code should generate a zigzag something like this

Please enter the maximum field width: 9
Enter the microseconds to pause between asterisks: 50000
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
 *
  *
   *
    *
     *
      *
       *
        *
       *
      *
     *
    *
   *
  *
 *
*
^C
my code below kinds of works but i don't know what i'm doing wrong any help would be great thanks

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

int main(void)
{

  int width;
  int interval;
  cout <<"Please enter the maximum field width: ";
  cin>> width;
  cout <<"Enter the micrososeconds to pause between asterisks: ";
  cin >> interval;

int n =1;

while (n <=width)
{
   cout <<(setw(n)) << "*" <<endl;
   n++;

}

while (true)

{

for(n =1; n<=width; n++)
cout << setw(n) <<"*" <<endl;

for(n=width; n > 1; n--)
cout << setw(n) <<"*" <<endl;
usleep(interval);


}

}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of businessesatoz
businessesatoz

ASKER

thank you all for your help.
How could this program be stopped by entering Ctrl+C?