Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c++ zigzag pattern setw

Posted on 2011-09-15
4
Medium Priority
?
1,527 Views
Last Modified: 2013-03-08
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

0
Comment
Question by:businessesatoz
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 750 total points
ID: 36545343
Well, the only thing that is wrong is that you are printing the 'topmost' star twice, so when counting doen, start at 'width - 1', i.e.
#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 - 1; n > 1; n--) // <-- here
cout << setw(n) <<"*" <<endl;
usleep(interval);


}

return 0; // that should be added as well
}

Open in new window

0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 750 total points
ID: 36547320
You have not explained your problem properly.
When I run your program, I get the following o/p:

Please enter the maximum field width: 5
Enter the micrososeconds to pause between asterisks: 100000
*
 *
  *
   *
    *
*
 *
  *
   *
    *
   *
  *
 *
*
I see that, in the beginning, you are having one slope and the opposite slope is not there.
After that, everything is ok.
If this is the problem, then remove the following lines (from line#18 to 23 in your post):
 
while (n <=width)
{
   cout <<(setw(n)) << "*" <<endl;
   n++;

}

Open in new window


And your code runs forever.
I think, you are doing that on purpose.
0
 

Author Closing Comment

by:businessesatoz
ID: 36558071
thank you all for your help.
0
 

Expert Comment

by:DK88
ID: 38967524
How could this program be stopped by entering Ctrl+C?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question