I'm attempting to create a computer program that prints a triangle of digits, as well as a "ruler" that measures the user specified width of the screen, showing the width in digits. The ruler must have a tens row and a ones row.

a) the screen width (maximum 80)
b) the width of the triangle.
c) the position of the triangle relative to the left margin.

The '1' column of the triangle must be printed at the given position on the screen. If the screen width is not wide enough to fit the entire triangle, the triangle must be clipped. In the extreme case, if the position is greater than the screen width, nothing is drawn (the entire triangle is clipped).

Here's a couple examples:
(Sorry about the code tags, it's the only way I could show the correct spacing)

Example one:

``````Enter screen width: 20
Enter triangle width: 4
Enter position: 7
1         2
12345678901234567890

1234
123
12
1``````

Example two:
Enter screen width: 10
Enter triangle width: 4
Enter position: 8

``````1
1234567890

123
123
12
1``````

Example three:
Enter screen width: 10
Enter triangle width: 4
Enter position: 9

``````1
1234567890

12
12
12
1``````

So, here's my problem. I've figured out how to properly print the "rulers" and the triangle. But, I can't figure out how to position the triangle, or clip the triangle if it is too wide. My teacher mentioned that it would require a 'For' loop nested within another for loop. How could I do this? I put a note in the code where I think the for loop should go, am I right?

Here's the code I have so far:

``````#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

int screenWidth, triWidth, position;

cout << "Enter Screen Width: ";
cin >> screenWidth;
cout << "Enter Triangle Width: ";
cin >> triWidth;
cout << "Enter Position: ";
cin >> position;

int ruler = (screenWidth / 10);

for (int topRuler = 1; topRuler <= ruler; topRuler++)
{
cout << "         " << topRuler; // Prints the 1st ruler
}
cout << endl;

int showNextRuler;
for (int nextRuler = 1; nextRuler <= screenWidth; nextRuler++)
{
if (nextRuler % 10 == 0)
{
showNextRuler = 0;
}
else if ((nextRuler > 10) && (nextRuler < 20))
{
showNextRuler = (nextRuler - 10);
}
else if ((nextRuler > 20) && (nextRuler < 30))
{
showNextRuler = (nextRuler - 20);
}
else if ((nextRuler > 30) && (nextRuler < 40))
{
showNextRuler = (nextRuler - 30);
}
else if ((nextRuler > 40) && (nextRuler < 50))
{
showNextRuler = (nextRuler - 40);
}
else if ((nextRuler > 50) && (nextRuler < 60))
{
showNextRuler = (nextRuler - 50);
}
else  if ((nextRuler > 60) && (nextRuler < 70))
{
showNextRuler = (nextRuler - 60);
}
else if ((nextRuler > 70) && (nextRuler < 80))
{
showNextRuler = (nextRuler - 70);
}
else if (nextRuler >= 80)
{
showNextRuler = 0;
}
else if (nextRuler < 10)
{
showNextRuler = nextRuler;
}

cout << showNextRuler;  // Prints the 2nd ruler
}
cout << endl;
cout << endl;

//
// GOOD UP TO HERE
//

for (int i = triWidth; i >= 1; i--)
{
for (int triangle = 1; triangle <= i; triangle++)
// THIS is where I think I need to put the loop to Position and Clip the triangle.
// Am I right? If so, how would it work?
{
int showTriangle;

if (triangle % 10 == 0)
{
showTriangle = 0;
}
else if ((triangle > 10) && (triangle < 20))
{
showTriangle = (triangle - 10);
}
else if ((triangle > 20) && (triangle < 30))
{
showTriangle = (triangle - 20);
}
else if ((triangle > 30) && (triangle < 40))
{
showTriangle = (triangle - 30);
}
else if ((triangle > 40) && (triangle < 50))
{
showTriangle = (triangle - 40);
}
else if ((triangle > 50) && (triangle < 60))
{
showTriangle = (triangle - 50);
}
else  if ((triangle > 60) && (triangle < 70))
{
showTriangle = (triangle - 60);
}
else if ((triangle > 70) && (triangle < 80))
{
showTriangle = (triangle - 70);
}
else if (triangle >= 80)
{
showTriangle = 0;
}
else if (triangle < 10)
{
showTriangle = triangle;
}
cout << showTriangle; // Prints the Triangle
}
cout << endl;
}
}``````

To position the triangle is as easy as printing out as many spaces as the position for each line:

``for (int x = 0; x < position; ++x) cout.put(' ');``

Clipping the triangle is only a bit harder. Calculate the current position and if it exceeds the …

Is there any way that I can clip the triangle without creating a new For statement? Could I put the break statement into one of the loops I already have?

My assignment specifies that I can't use a For loop specifically for clippin the triangle... it …

## All 8 Replies

Nobody?

Still can't figure out how to position and clip the triangle.

To position the triangle is as easy as printing out as many spaces as the position for each line:

``for (int x = 0; x < position; ++x) cout.put(' ');``

Clipping the triangle is only a bit harder. Calculate the current position and if it exceeds the width, break your printing loop for that line:

``````for (int x = 0; x < twidth; ++x)
{
if (x + position >= swidth) break;
else cout << x + 1;
}``````

Is there any way that I can clip the triangle without creating a new For statement? Could I put the break statement into one of the loops I already have?

My assignment specifies that I can't use a For loop specifically for clippin the triangle... it must be in one of the other For statements.

Is there any way that I can clip the triangle without creating a new For statement? Could I put the break statement into one of the loops I already have?

My assignment specifies that I can't use a For loop specifically for clippin the triangle... it must be in one of the other For statements.

Please post your existing code and the updates that you have done. So it will be easier to come up with an answer. And also tell us which forloop to avoid.

Here's my updated code... Which of the for loops should I put the break statement within?\

Oh, and to answer your question, you don't need to avoid any of the For loops, but you can not create a New For loop to clip the triangle. The code to clip the triangle (break statement) must sit within one of the For loops I've already created (and not use its own For loop.)

``````#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{

int screenWidth, triWidth, position;

cout << "Enter Screen Width: ";
cin >> screenWidth;
cout << "Enter Triangle Width: ";
cin >> triWidth;
cout << "Enter Position: ";
cin >> position;

int ruler = (screenWidth / 10);

for (int topRuler = 1; topRuler <= ruler; topRuler++)
{
cout << "         " << topRuler; // Prints the 1st ruler
}
cout << endl;

int showNextRuler;
for (int nextRuler = 1; nextRuler <= screenWidth; nextRuler++)
{
if (nextRuler % 10 == 0)
{
showNextRuler = 0;
}
else if ((nextRuler > 10) && (nextRuler < 20))
{
showNextRuler = (nextRuler - 10);
}
else if ((nextRuler > 20) && (nextRuler < 30))
{
showNextRuler = (nextRuler - 20);
}
else if ((nextRuler > 30) && (nextRuler < 40))
{
showNextRuler = (nextRuler - 30);
}
else if ((nextRuler > 40) && (nextRuler < 50))
{
showNextRuler = (nextRuler - 40);
}
else if ((nextRuler > 50) && (nextRuler < 60))
{
showNextRuler = (nextRuler - 50);
}
else  if ((nextRuler > 60) && (nextRuler < 70))
{
showNextRuler = (nextRuler - 60);
}
else if ((nextRuler > 70) && (nextRuler < 80))
{
showNextRuler = (nextRuler - 70);
}
else if (nextRuler >= 80)
{
showNextRuler = 0;
}
else if (nextRuler < 10)
{
showNextRuler = nextRuler;
}

cout << showNextRuler;  // Prints the 2nd ruler
}
cout << endl;
cout << endl;

// This is where the code begins to create, space, and clip the triangle.
// Which of the following For loops does the break statement to clip the triangle need be inside?

for (int i = triWidth; i >= 1; i--)
{

for (int y = 1; y < position; y++)
{
cout.put (' '); // Prints spaces before the triangle
}

for (int triangle = 1; triangle <= i; triangle++)
{
int showTriangle;

if (triangle % 10 == 0)
{
showTriangle = 0;
}
else if ((triangle > 10) && (triangle < 20))
{
showTriangle = (triangle - 10);
}
else if ((triangle > 20) && (triangle < 30))
{
showTriangle = (triangle - 20);
}
else if ((triangle > 30) && (triangle < 40))
{
showTriangle = (triangle - 30);
else if ((triangle > 40) && (triangle < 50))
{
showTriangle = (triangle - 40);
}
else if ((triangle > 50) && (triangle < 60))
{
showTriangle = (triangle - 50);
}
else  if ((triangle > 60) && (triangle < 70))
{
showTriangle = (triangle - 60);
}
else if ((triangle > 70) && (triangle < 80))
{
showTriangle = (triangle - 70);
}
else if (triangle >= 80)
{
showTriangle = 0;
}
else if (triangle < 10)
{
showTriangle = triangle;
}
cout << showTriangle; // Prints the Triangle
}
cout << endl;
}
}``````

Okay, forget my last post, I've figured pretty much every thing out. I only have one problem left. When my program clips the triangle, it clips it two lines earlier than it should.

Why does it stop printing 2 lines early?

Here's what the output looks like, followed by my code. Any quick help would be appreciated.

``````Enter Screen Width: 30
Enter Triangle Width: 20
Enter Position: 21
1         2         3
123456789012345678901234567890

12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
12345678
1234567
123456
12345
1234
123
12
1``````

So there you can see that it stops printing two lines early.
Here's my code, where am I going wrong?

``````#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int screenWidth, triWidth, position;

cout << "Enter Screen Width: ";
cin >> screenWidth;
cout << "Enter Triangle Width: ";
cin >> triWidth;
cout << "Enter Position: ";
cin >> position;

int ruler = (screenWidth / 10);

for (int topRuler = 1; topRuler <= ruler; topRuler++)
{
cout << "         " << topRuler; // Prints the 1st ruler
}
cout << endl;

int showNextRuler;
for (int nextRuler = 1; nextRuler <= screenWidth; nextRuler++)
{
int showNextRuler = (nextRuler % 10);
cout << showNextRuler;  // Prints the 2nd ruler
}
cout << endl;
cout << endl;

for (int i = triWidth; i >= 1; i--)
{

for (int y = 1; y < position; y++)
{
cout.put (' '); // Prints spaces before the triangle
}

for (int triangle = 1; triangle <= i && (triangle + position) <= screenWidth; triangle++)
{
int showTriangle;

showTriangle = (triangle % 10);

if (triangle + position >= screenWidth)
{
break; // Clips the Triangle - Why does it begin clipping 2 spaces early?
}
else
{
cout << showTriangle;
}
}
cout << endl;
}
}``````

This assignment is due in 2 hours... if someone could please quickly tell me why the triangle stops printing two lines early, I'd be really appreciative.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.