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.
#include <iostream>
usingnamespace 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;
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.
#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.
#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;
}
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
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.