loop question

hi I am trying to loop this code for a user defined period can anyone suggest how i would do this? i am thinking some sort of look but i'm not sure how to implement it.

thanks in advance

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
 #include <iostream>

using namespace std;

int main()
{
	int rows, repeat;
	cout << "Enter the number of rows in the triangle" << endl;
	cin >> rows;
	cout << "how many times to repeat triangle" << endl;
	cin >> repeat;

	for (int i = 1; i <= rows; i++) {//outer for loop
		for (int j = 1; j <= i; j++) {//inner for loop 
			cout << "*";//print star for pyramid top half
		}
		cout << endl;  //move to next line
	}
	for (int i = rows; i >= 1; i--) {//outer for loop
		for (int j = 1; j <= i; j++) {//inner loop
			cout << "*"; //print star for bottom half
		}
		cout << endl;
	}	/* now to make it repeat
						for (int repeat = 0; repeat < 20; repeat++)*/
		
				
	return 0;
Hello farmerjohn,

I do not quite follow what you mean by repeat. What i have seen with this type of program is either one triangle or a triangle with its mirror image below the first.

This idea might help:
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
46
47
48
49
50
51
52
#include <iostream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/



int main()
{
	int rows{}, width{}, repeat{}, count{};

	std::cout << "Enter the heigth of the triangle" << std::endl;
	std::cin >> rows;

	std::cout << "Enter the width of the triangle" << std::endl;
	std::cin >> width;

	std::cout << "how many times to repeat triangle" << std::endl;
	std::cin >> repeat;

	do
	{
		for (int i = 1; i <= rows; i++)
		{//outer for loop
			for (int j = 1; j <= i; j++)
			{//inner for loop 
				std::cout << "*";//print star for pyramid top half
			}

			std::cout << std::endl;  //move to next line
		}

		for (int i = rows; i >= 1; i--)
		{//outer for loop
			for (int j = 1; j <= i; j++)
			{//inner loop
				std::cout << "*"; //print star for bottom half
			}

			std::cout << std::endl;
		}

		count++;
	} while (count < repeat);

	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue :";
	std::cin.get();

	return 0;
}


I have not tested this yet, but should give you an idea if I follow what you are asking.

I noticed that the for loops print stars, but you have neglected to account for the spaces that would make it look like a triangle.

I have not tested your code yet, but the for loops look like it would pring a filled in square or rectangle.

Hope that helps,

Andy

Edit: Changed a missed variable in the inputs.
Last edited on
Hello farmerjohn,

Before I tested the program I was thinking of the triangle in a different form.

After testing the program I see what you are doing and it works.

I think the do/while loop is what you are looking for.

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
46
47
#include <iostream>
#include <limits>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	int rows{}, count{}, repeat{};

	std::cout << "Enter the number of rows in the triangle" << std::endl;
	std::cin >> rows;
	std::cout << "how many times to repeat triangle" << std::endl;
	std::cin >> repeat;
	std::cout << std::endl;

	do
	{
		for (int i = 1; i <= rows; i++)
		{//outer for loop
			for (int j = 1; j <= i; j++)
			{//inner for loop 
				std::cout << "*";//print star for pyramid top half
			}
			std::cout << std::endl;  //move to next line
		}

		for (int i = rows; i >= 1; i--)
		{//outer for loop
			for (int j = 1; j <= i; j++)
			{//inner loop
				std::cout << "*"; //print star for bottom half
			}
			std::cout << std::endl;
		}

		count++;
		std::cout << std::endl;  // <--- Puts a blank line between repeats.
	} while (count < repeat);

	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}


Hope that is what you are looking for,

Andy
Hi Andy,
Sorry I’m not always the best at explaining myself. What I am trying to do is a program which makes a triangle made of * and will continue making copies of the same triangle for a user defined amount of times.

Looking at what you have suggested it looks to solve what I am trying to achieve. I am now going to try and tweak that so I can make the user define if the program makes the same sized triangles or with every new triangle they get progressively bigger. From my thinking I need to add in a else (rows =++) somewhere but I will play around with it before I send out another distress flair!

Thanks for responding and your help I really appreciate it

John
Last edited on
Hello farmerjohn,

That is easy. After the line count++; add a linerows++; or rows += 2; or whatever number you like. The next time through the do/while loop the triangle will be bigger.

Andy
Topic archived. No new replies allowed.