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.
The program reads three integers:
a) the screen width (maximum 80)
b) the width of the triangle.
c) the position of the 1 column 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 of what the output should look like:
(Sorry about the code tags, it's the only way I could show the correct spacing)
Example one:
Code:
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
Code:
1
1234567890
123
123
12
1
I've gotten the triangle and ruler to print correctly, and I've figured out how to properly put spaces before the triangle.
My problem is with the code for Clipping the end of the triangle off. For some reason, my program stops printing the triangle two columns early.
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.
Code:
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?
Code:
#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;
}
}