Print CROSS Pattern (with full commenting)

09:21 0 Comments A+ a-

Problem:
                     Print the pattern of a cross on console.
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int n, l, s, t; //n is integer taken, l is used to print upper and lower half, s is used to set the value of setw of upper half right *, t is also used to set the value of setw of lower half right *
cout << "Please enter the value of size of Cross (must be odd and >=3)";
cin >> n;
while (n%2==0 || n<3)
{
cout << "Invalid input for Size of pattern of Cross" << endl;
cout << "Please enter the value of size of Cross again (must be odd and >=3)";
cin >> n;
}
l = n / 2; //l is n/2 as i want to print upper and lower half and the middle line separately
int a = 1; //a is taken to set the value of setw on the left hand side *
s = n - 1; //s is taken 1 less than n because 1 * is already printed
t = 2; //t is taken 2 as because after the middle line the * are two spaces away from each other.

for (int i = 0; i<l; i++) //for loop to control upper half rows i.e n/2 excluding middle line
{
cout << setw(a) << "*"; //setw is taken 1 as the upper left starric is printed on the full left, and then 1 is added in "a" to diplace * by 1 place after
cout << setw(s) << "*"; //
cout << endl;
a++;
s = s - 2; //2 is subtracted from s each time this loop runs as the upper left * moves right by 1 value and right * moves left by 1 each time
}
cout << setw((n / 2) + 1) << "*" << endl; //to display center row
int b = n / 2; //b is taken n/2 as the * lower left to centre row is on setw(n/2)
for (int i = 0; i<l; i++) //for loop to control lower half rows i.e n/2 excluding middle line
{
cout << setw(b) << "*";
cout << setw(t) << "*";
cout << endl;
b--; //1 is subtracted from b each time as the the lower left * moves left in each turn
t += 2; //2 is added in t as the lower right and left * moves away by value 1 each so 1+1=2
}




system("pause");
return 0;
}