This program is using setw() and setfill() it supposed to output the stars I have included the sample output at the end of my project

I hope u gona be able to read the code cause my coding does not print the stars the way they have to be instead it prints 2 more stars in the middle and If u cud possible be able to detect the logic error please help I have included the code

#include "stdafx.h"
#include <iostream>
#include <iomanip>
 using namespace std;

void main()
{
	int space = 2;
	int counter = 19;
	int count = 1;
	
    cout << "Assignment 1 --- Question 2.2" << endl;
	cout << endl;
	cout << setfill('*') << setw(23) << "*"  << endl;
	cout << setfill('*') << setw(23) << "*"  << endl;

	for(int x = 1; x <= 10; x++)
	{
		counter--;
		cout << "**";
		cout  << setfill(' ') << setw(count);
		cout << setw(count) << "*" << setfill(' ');
		cout << setw(counter) << setfill(' ') << "*";
		cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
		count++;
		space++;
		counter--;
	}

	for(int x = 1; x <= 10; x++)
	{
		space--;
		count--;
		counter++;
		cout << "**";
		cout  << setfill(' ') << setw(count);
		cout << setw(count) << "*" << setfill(' ');
		cout << setw(counter) << setfill(' ') << "*";
		cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
		counter++;
	}
	cout << setfill('*') << setw(23) << "*"  << endl;
	cout << setfill('*') << setw(23) << "*"  << endl;
	cout << endl;
}


//This is what I am suppose to produce but instead it produces two more stars in the middle
//sample output
**************
**************
***        ***
** *      * **
**   *   *  **
**    * *   **
**     *    **
**    * *   **
**  *    *  **
** *      * **
***        ***
**************
**************

Recommended Answers

All 8 Replies

All you have to do is replact <= 10 to < 10 in each of the two loops for(int x = 1; x < 10; x++)

Simple solution: Make the center line a special case outside of the loops and change the condition of the loops to be < 10 rather than <= 10:

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

void main()
{
  int space = 2;
  int counter = 19;
  int count = 1;

  cout << "Assignment 1 --- Question 2.2" << endl;
  cout << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;

  for(int x = 1; x < 10; x++)
  {
    counter--;
    cout << "**";
    cout  << setfill(' ') << setw(count);
    cout << setw(count) << "*" << setfill(' ');
    cout << setw(counter) << setfill(' ') << "*";
    cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
    count++;
    space++;
    counter--;
  }

  for(int x = 1; x < 10; x++)
  {
    space--;
    count--;
    counter++;
    cout << "**";
    cout  << setfill(' ') << setw(count);
    cout << setw(count) << "*" << setfill(' ');
    cout << setw(counter) << setfill(' ') << "*";
    cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
    counter++;
  }
  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << endl;
}

>All you have to do is replact <= 10 to < 10 in each of the two loops
That doesn't quite solve the problem.

Thanx a lot guys

but......
Nurue I dont think I understand what do you mean about the center line and making it a special case outside the for loop

Can u please clarify that or explain a bit

>Nurue
Narue.

>I dont think I understand what do you mean about the
>center line and making it a special case outside the for loop
Hmm, I can see why when I posted the wrong code... This is what I meant to post:

#include <iostream>
#include <iomanip>

using namespace std;

void main()
{
  int space = 2;
  int counter = 19;
  int count = 1;

  cout << "Assignment 1 --- Question 2.2" << endl;
  cout << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;

  for(int x = 1; x < 10; x++)
  {
    counter--;
    cout << "**";
    cout << setfill(' ') << setw(count);
    cout << setw(count) << "*" << setfill(' ');
    cout << setw(counter) << setfill(' ') << "*";
    cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
    count++;
    space++;
    counter--;
  }

  cout << "**";
  cout << setfill(' ') << setw(count);
  cout << setw(count) << "*" << setfill(' ');
  cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;

  for(int x = 1; x < 10; x++)
  {
    space--;
    count--;
    counter++;
    cout << "**";
    cout << setfill(' ') << setw(count);
    cout << setw(count) << "*" << setfill(' ');
    cout << setw(counter) << setfill(' ') << "*";
    cout << setw(space) << setfill('*') << setfill(' ') << "**" << endl;
    counter++;
  }

  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << setfill('*') << setw(23) << "*"  << endl;
  cout << endl;
}

when I ran the program it worked as written except for changin <= 10 to < 10, as I stated in my oroginal post.
[edit]Oops! not it doesn't work right. [/edit]

Thanx guys that is sorting it all but I will mark it as solved after I got a chance to run it,

Thank you guys for your time it is really appreciated

Ok guys the project is just perfect, and how I wish I had known this before anyway
thats not the case..... There is alwayz a next time...... hey:)

Thanx a lot guyz

>There is alwayz a next time
Every bit of code you write is a lesson for the next project. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.