Right justified triangle help please

I can get my triangle to be left justified, but i have tried almost every combination of using spaces to move my triangle over to be right justified and i am absolutely stuck, please help.

#include <iostream>
using namespace std;

int main ()
{
int value,rowcount=0, starcount=0,initrow=0,starlinesize,spacecount;

cout << "Enter a positive integer: " <<endl;
cin >> value;

while (value < 0)
{
cout << "That was not a positive integer, re-enter: " <<endl;
cin >> value;
}

starlinesize=value;


for(rowcount=0; rowcount < value; rowcount++)
{
for (spacecount=rowcount; spacecount < value; spacecount++)
cout << ' ';
for (starcount=0; starcount < starlinesize; starcount++)
cout << '*';



starlinesize=starlinesize-1;
cout<< endl;


}



return 0;
}
Last edited on
In such frustrating cases, it is sometimes caused by confusion on how the current program works.

1) Look at your loops and determine the details on how it works left-justified.

2) Formulate a plan on how you want the program to build the triangle right-justified.

3) Implement the plan by adjusting the existing code.

Reply back on where you get stuck.

Here's one using while... extrapolate for for()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

int main ()
{
  int lines = 0;

  std::cout << "Enter a positive integer: " << std::endl;
  std::cin >> lines;

  while (lines < 0)
  {
    std::cout << "That was not a positive integer, re-enter: " << std::endl;
    std::cin >> lines;
  }

  while(lines)
  {
    static int starcount = 1, spacecount = lines;
    int spaces = spacecount, stars = starcount;
    while(spaces)
    {
      std::cout << ' ';
      --spaces;
    }
    while(stars)
    {
      std::cout << '*';
      --stars;
    }
    std::cout << std::endl;
    starcount++;
    spacecount --;
    lines--;
  }
  return 0;
}
Topic archived. No new replies allowed.