HELP PLEASE! Astrix and triangles..

Nov 9, 2016 at 11:05pm
How do i make the astrix do this?... I know I have to add spaces somewhere Im just not sure where... i have the first two triangles down .. the third and fourth one im struggling with.. and also this is according to what the user puts in .. so for example this is for 5.

*****
****
***
**
*
*****
+****
++***
+++**
++++*

+++++*+++++
++++***++++
+++*****+++
++*******++
+*********+


+++++*+++++
++++*+*++++
+++*+++*+++
++*+++++*++
+*********+

(without the +s)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;

int main() {
int num;
   
   do
   {
      cout << "Enter a number between 1 and 15: " << endl;
      cin >> num;
      
      if (num > 15 || num < 1)
      {
         cout << "Error! The number must be between 1 and 15." << endl;
      }
   }   
   while (num > 15 || num < 1);
   
   for(int pattern = 0; pattern < 2; pattern++)
   {
      for (int i = num; i > 0; i--)
      {
         for (int j = 0; j < i; j++)
         {
            cout << "*";
         }
         cout << endl;
       }
    }
    
	return 0;
}
Last edited on Nov 10, 2016 at 10:46pm
Nov 9, 2016 at 11:24pm
Seems to work fine for me.

Enter a number between 1 and 15: 
5
*****
****
***
**
*
*****
****
***
**
*
Nov 9, 2016 at 11:36pm
@intelgralfx
i meant
*****
****
***
**
*
*****
+****
++***
+++**
++++*

(without the pluses, with spaces instead)
Nov 9, 2016 at 11:45pm
1
2
3
4
5
6
7
8
9
10
for( int i = num; i > 0; i-- )
{
    for( int s = 0; s < num - i; s++ ) {
        cout << " ";
    }
    for( int j = 0; j < i; j++ ) {
        cout << "*";
    }
    cout << endl;
}


Enter a number between 1 and 15: 
5
*****
****
***
**
*
*****
 ****
  ***
   **
    *


Might be useful: http://www.cplusplus.com/forum/general/200987/
Last edited on Nov 9, 2016 at 11:53pm
Nov 10, 2016 at 12:14am
@intelgralfx .. wait now its only showing the bottom triangle..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

int main() 
{
	int num;
	do
	{
      cout << "Enter a number between 1 and 15: " << endl;
      cin >> num;
      
      if (num > 15 || num < 1)
      {
         cout << "Error! The number must be between 1 and 15." << endl;
      }
	}
	while (num > 15 || num < 1);
	for (int i = num; i > 0; i-- )
	{
		for (int s = 0; s < num - i; s++)
   		{
   			cout << " ";
   		}
		for (int j = 0; j < i; j++)
		{
			cout << "*";	
   		}
   		cout << endl;
	}
   return 0;
}
Nov 10, 2016 at 12:22am
That's because the spacing loop runs on every iteration. If you just want it on the second triangle, just add an if statement.
Nov 10, 2016 at 12:35am
How do I make the astrix do this?... I know I have to add spaces somewhere I'm just not sure where...
*****
****
***
**
*
*****
****
***
**
*


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main()
{
	int N;
	int i, j, k;
	do
	{
		cout << "Enter a number from 1 - 15 : "; 
		
		if(cin >> N && N >= 1 && N <= 15)
		{
			// Do nothing
		}
		else
		{
			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');
			}

			N = 20;
			cout << "Invalid number. Try again\n\n";
		}
	} while(N < 1 || N > 15);

	// Two triangles
	for(k = 0; k < 2; k++)
	{
		for(i = N; i >= 1; i--)
		{
			for(j = i; j >= 1; j--) cout << '*'; cout << endl;
		}
	}
	
	cin.get();
	return 0;
}


Enter a number from 1 - 15 : 5
*****
****
***
**
*
*****
****
***
**
*


http://cpp.sh/3spg2
Nov 10, 2016 at 1:50am
would it be this @integralfx?
1
2
3
4
5
if (pattern = 1) {
for( int s = 0; s < num - i; s++ ) {
        cout << " ";
    }
}
Nov 10, 2016 at 5:51am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

int main()
{
	int N;
	int i, j;
	do
	{
		cout << "Enter a number from 1 - 15 : "; 
		
		if(cin >> N && N >= 1 && N <= 15)
		{
			// Do nothing
		}
		else
		{
			if(!cin)
			{
				cin.clear();
				cin.ignore(1000, '\n');
			}

			N = 20;
			cout << "Invalid number. Try again\n\n";
		}
	} while(N < 1 || N > 15);

	// Triangle 1
	for(i = N; i >= 1; i--)
	{
		for(j = i; j >= 1; j--) cout << '*'; cout << endl;
	}

	// Triangle 2
	for(i = N; i >= 1; i--)
	{
		for(j = N; j > i; j--) cout << ' ';
		for(j = i; j >= 1; j--) cout << '*'; cout << endl;
	}
	
	cin.get();
	return 0;
}


Enter a number from 1 - 15 : 5
*****
****
***
**
*
*****
 ****
  ***
   **
    *

http://cpp.sh/4inji
Topic archived. No new replies allowed.