# Help making Triangle

• 02-11-2010
jensklemp
Help making Triangle
Hi again all,

I am having some troubles making this program. Here is the assignment:

Code:

```Write a computer program that prints a triangle of digits. Your program must read two positive integers as follows: the width of the triangle, and the position of the triangle relative to the left margin (the amount to indent the triangle, in other words). You can assume that the user will always enter non-zero positive integers. The triangle is drawn upside-down using single digit integers. For example, a width 3 indent 3 triangle looks like this:   123   12   1 ... and a width 13 triangle indent 0 triangle looks like this: 1234567890123 123456789012 12345678901 1234567890 123456789 12345678 1234567 123456 12345 1234 123 12 1```
and here is my code:
Code:

```#include <iostream> using namespace std; int main () {         int width = 0;         int indent = 0;         int digit = 1;         cout<<"Enter the width of the triangle: ";         cin>>width;         cout<<"Enter the position relative to the left margin (the indent): ";         cin>>indent;         for (int x = 0; x < width; x++)         {                 for (int i = 0; i < indent; i++)                 {                         cout<<" ";                 }                 for (int w = 0; w < width; w++)                 {                         cout<<digit%10;                         digit++;                 }                 width--;                 digit = 1;                 cout<<endl;         }         return 0; }```
When the program is run it looks like this:
Code:

```Enter the width of the triangle: 5 Enter the position relative to the left margin (the indent): 3   12345   1234   123```
I understand why it quits short but am unable to figure out a fix for the problem.

Anyone got some suggestions? Thank you!
• 02-11-2010
Salem
Make your outer loop count backwards.

> int main = 0;
And width is undeclared - are you sure this is REALLY your code?
• 02-11-2010
jensklemp
Sorry somehow my brain told me to write main instead of width which I wanted.
Why would I want to count backwards?
• 02-11-2010
Salem
> Sorry somehow my brain told me to write main instead of width which I wanted.
How about just copy/paste from your actual source code?

It saves you time, and we don't waste more time pointing out trivial mistakes only for you to say "oh, that's not in the real code".

> Why would I want to count backwards?
Because the lines get shorter (perhaps)
• 02-11-2010
jensklemp
YAY! I got it, sorry to waste your time. I created a new variable and called it length, this way it didn't affect the outer loop. Now it looks like this:

Code:

```#include <iostream> using namespace std; int main () {         int width = 0;         int indent = 0;         int digit = 1;         int length = 0;         cout<<"Enter the width of the triangle: ";         cin>>width;         cout<<"Enter the position relative to the left margin (the indent): ";         cin>>indent;         length = width;         for (int x = 0; x < width; x++)         {                 for (int i = 0; i < indent; i++)                 {                         cout<<" ";                 }                 for (int l = 0; l < length; l++)                 {                         cout<<digit%10;                         digit++;                 }                 length--;                 digit = 1;                 cout<<endl;         }         return 0; }```