Decending Triangle Question

Hello everyone. I am new to the site and have a question. I am trying to write a descending triangle program with asterisks in C++. I have everything working but my output is not centered correctly and it is driving me nuts. I have played with variables and the loops and I just can't get it. Any help would be appreciated. Here is what I got for code:

#include <iostream>
using namespace std;

int main ()
{
int N;

cout << "Enter a whole number: " << endl;
cin >> N;

double S = 10; // used to push triangle form left edge

for(double forOut = N; forOut >= 0; forOut--)
{

for(double x = 0; x < (S - forOut / 2); ++x)
cout << ' ';

for(int forIn = 1; forIn <= forOut; forIn++)
cout << "*";
cout << endl;
}

system ("pause");
return 0;
}

If I enter 6 my output is:
----******
-----*****
-----****
------***
------**
-------*
(Slashes added to show alignment issues)

How do I get this to line up?

Thanks for your time.
On the first line, you want 0 spaces and N asterisks? Then
On the second line, you want 1 space and N-2 asterisks.
On the third line, you want 2 spaces and N-4 asterisks.

Look for the pattern.

There is no need for division or floating point arithmetic in this program.
Thanks for the input jsmith.
I want the output to have 1 asterisk on the bottom line, 3 asterisk's on the second line, 5 asterisk's on the third up to N lines. I need to increment the asterisk's by 2 each loop but I can't figure how. I have change and/or incremented every variable but no luck. I am about to move on to something different.
Any advice? - besides moving on to something different :) -
Thanks again
Since your program must output the top line first, you should think about it the
way I wrote above ... first line first.

If you want N lines of output, then on the first line (line zero) there will be 0 leading
spaces followed by ( N - 0 ) * 2 + 1 asterisks. On the second line (line one) there will be
1 space followed by ( N - 1 ) * 2 + 1 asterisks.

You'll want one big loop that counts the number of lines output so far (N in my above
formulas). Inside that loop, you'll need two independent for loops: the first one will
output the correct number of spaces, and the second will output the correct number
of asterisks.

This should be everything you need. I can't give you any more hints without actually
writing the code for you. Why don't you try implementing what I wrote and post your
code if you aren't able to get it working.

Thanks alot jsmith for your help. I got it :)

What do you think?

#include <iostream>
using namespace std;

int main ()
{
int N;

cout << "Enter a whole number: " << endl;
cin >> N;

int spaces = 0;
int printStars = 1;

for(int forOut = N; forOut >= 0; forOut--)
{
for(int centerTriangle = 0; centerTriangle <= spaces; centerTriangle++)
cout << ' ';
spaces++;
for(int forIn = 1; forIn <= (N - printStars) * 2 + 1; forIn++)
cout << '*';
cout << endl;
printStars++;
}

system ("pause");
return 0;
}

Thanks again for your time. I will have more questions as time goes on. I am just starting out in c++ and I am trying to teach myself.
Topic archived. No new replies allowed.