0

The idea is to print 4 shapes, the first two shapes print fine, and the next two shapes using setw are meant to be the mirrors but still printed bellow as they are.

My understanding was that setw made a kind of text box that started outputting from right to left from the text location specified in the parameters, and it works for other examples I have tried. But for some reason when passed through these for loops it just adds tab spaces of the set amount and prints on the wrong side of the setw location.

#include <conio.h>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
   int x = 1;
   for (int i = 0; i < 9; i++)
   {
      for (int i = 1; i <= x; i++)
         cout << "*";
      x++;
      cout << endl;
   }

   cout << endl;
   x = x - 1;

   for (int i = 0; i < 9; i++)
   {
      for (int i = 1; i <= x; i++)
         cout << "*";
      x--;
      cout << endl;
   }

   cout << endl;
   for (int i = 0; i < 9; i++)
   {
      cout << setw(10);
      for (int i = 1; i <= x; i++)
         cout << "*";
      x++;
      cout << endl;
   }

   cout << endl;
   for (int i = 0; i < 9; i++)
   {
      cout << setw(10);
      for (int i = 1; i <= x; i++)
         cout << "*";
      x--;
      cout << endl;
   }
   _getch();
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Max Mcgregor
  • 101
  • 9
  • 4
    Could you possibly include the output of your code and compare it to what your desired output is? – Burgan Oct 07 '15 at 06:33

2 Answers2

3

I not able to see your output but this information may help.

setw is used to specify the minimum space for next numeric or string value. This means it will add some padding if the space indicated is greater than that of the numeric value or string.

Most importantly setw does not change the internal state of the output stream so it only determines the size for the next input , meaning that it would only apply for the first iteration of your for loop.

MAG
  • 454
  • 5
  • 14
  • I cant format the output right for this website, the output is just a triangle made of * that, which is then flipped (this works) and both are then mirred – Max Mcgregor Oct 07 '15 at 08:29
1

You setw() once, and then output x times. setw() only affects the next output, i.e. the first character -- which is set right-to-left as you instructed -- with the remaining charaters appended to it.

So your inner loop (with a loop counter shadowing the outer one... shudder) cannot work as intended -- you need to print your shape line in one go for setw() to be effective. This can be done with a rather helpful std::string constructor:

basic_string( size_type count,
              CharT ch,
              const Allocator& alloc = Allocator() );

Constructs the string with count copies of character ch. The behavior is undefined if count >= npos.

(Source: cppreference.com)

And then there's the matter of the third shape having one line less than the others.

Fixed code:

#include <iostream>
#include <iomanip>
#include <string>

// <conio.h> is not available on non-Windows boxes,
// and if MSVC were smart enough to keep the console
// window open, this kludge wouldn't be necessary
// in the first place.
#ifdef _WIN32
#include <conio.h>
#endif

using namespace std;

int main()
{
   int x = 1;
   for (int i = 0; i < 9; i++)
   {
      cout << string( x, '*' ) << "\n";
      x++;
   }

   cout << endl;
   x = x - 1;

   for (int i = 0; i < 9; i++)
   {
      cout << string( x, '*' ) << "\n";
      x--;
   }

   cout << endl;

   for (int i = 0; i < 9; i++)
   {
      // increment first, or the loop will not print
      // the last line, making the third shape different.
      x++;
      cout << setw(10) << string( x, '*' ) << "\n";
   }

   cout << endl;

   for (int i = 0; i < 9; i++)
   {
      cout << setw(10) << string( x, '*' ) << "\n";
      x--;
   }

#ifdef _WIN32
   _getch();
#endif
}

This could be streamlined further by creating one string and then printing substrings of that in each loop (instead of creating a new temporary string each time), but I wanted to stay close to your original code.

DevSolar
  • 67,862
  • 21
  • 134
  • 209