Hi Guys,

I am writing a program which enables the user to enter a size for a square and then they can choose to output it in several ways, clear filled horizontal/verticals lines. The first two I have manged however the latter horizontal/vertical I cant quite get right, as they only work if the square size is 5. I understand this is because of where the asterisks are printed along x and y however I'm not sure how to write it so that it works no matter the size of the square eg. prints every other line.

Here is my code so far:

#include <iostream>
using namespace std;

int main ()   
{
	int x, y, i, type, option;

	cout << "Please enter square size: ";
	cin >> i;
	cout << "Please enter its type " << endl;
	cout << "   1: clear" << endl;
	cout << "   2: filled" << endl;
	cout << "   3: horizontal lines" << endl;
	cout << "   4: vertical lines" << endl;
	cout << "Type: ";
	cin >> type;
	cout << endl;

	if (type == 2){

		for (x=i; x>0; x--)
		{
			for (y=i; y>1; y--)  
				cout << "*"; 
			cout << "*" << endl;  
		}
		cout << endl;
	}

	else

		if (type == 1){

			for(y=0; y<i; y++)
			{
				cout << endl << endl;
				for(int x=0; x<i; x++)
				{
					if ((y==0 || y==i-1) || (x==0) || (x==i-1))
					{
						cout << "* ";
					}
					else cout<<"  ";
				}
			}
			cout << endl << endl;
		}
		
		else 
			if (type == 3)	{

				for(y=0; y<i; y++)
				{
					cout << endl << endl;
					for(int x=0; x<i; x++)
					{
						if ((y==0 || y==i-3) || y==i-1 || (x==0) || (x==i-1))
						{
							cout << "* ";
						}
						else cout <<"  ";
					}
				}
				cout << endl << endl;
			}

		else 
			if (type == 4)	{

				for(y=0; y<i; y++)
				{
					cout << endl << endl;
					for(int x=0; x<i; x++)
					{
						if ((y==0 || y==i-1) || (x==0) || (x==i-1) || (x==i-3))
						{
							cout << "* ";
						}
						else cout <<"  ";
					}
				}
				cout << endl << endl;
			}
	
			cout << "Draw another square?" << endl;
			cout << "    1: Yes" << endl;
			cout << "    2: No" << endl;
			cout << "Option: ";
			cin >> option;

}

Thankyou!

Recommended Answers

All 3 Replies

Post what the figures should look like. Are we talking about this? And you can't get the last two?

Clear:

*****
*   *
*   *
*   *
*****

Filled:

*****
*****
*****
*****
*****

Horiz. Lines:

*****



*****

Vertical Lines

*   *
*   *
*   *
*   *
*   *

Post what the figures should look like. Are we talking about this? And you can't get the last two?

Clear:

*****
*   *
*   *
*   *
*****

Filled:

*****
*****
*****
*****
*****

Horiz. Lines:

*****
*   *
*****
*   *
*****

Vertical Lines

*  *  *  *  *
*     *     *
*     *     *
*     *     *
*  *  *  *  *

The first two yes I have those sorted but I would want the second two to look like that. The problem I'm having is that the ones above work because they are 5 asterisks in size however when the user imputs their own number above 5 the asterisks are placed in the wrong position.

You're hard coding too much here:

if ((y==0 || y==i-3) || y==i-1 || (x==0) || (x==i-1))

Your test needs to be whether y is even or not. Say the number of lines is 13. You want lines 0, 2, 4, 6, 8, 10, and 12 to be full lines of asterisks and you want the other lines to be blank spaces except for the the end. So you need to test whether y is even or odd. Use the mod operator (%) to test for even or odd:

if ((y % 2 == 0) || (y == i-1) || (x==0) || (x==i-1))

Do something similar for the vertical lines, except use x.

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.