简体   繁体   中英

Spacing for Right aligned Triangle

Assignment:

Write a program that prompts the user for an integer value representing the height of a triangle. The program should then print out a triangle of O's of that height, with a vertically aligned right edge.

My problem:

I have figured out the code to execute a normal triangle, but I am having some difficulty on writing code to have spacing before my "0's" to make it aligned to the right.

#include <iostream>

using namespace std;

int main()
{
    int triHeight;
    int c = 0;
    int r = 0;
    int k = 0;

    cout << "Enter the triangle height: " << endl;
    cin >> triHeight;

    for (c = 0; c <= triHeight; c = c+1)
    {
        for (r = 0; r < c; r = r + 1)
        {
            cout << "0";
        }
        for (k = 0; k <= c; k = k - 1)
        {
            cout << " ";
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

You have two main problems :

  • you write the spaces after the 0 rather than before to have the 0 indented

  • for (k = 0; k <= c; k = k - 1) never ends up to the possible effect of an overflow

I also encourage you to check the result of >> to be sure a valid integer was enter so like if (!(cin >> triHeight)) cerr << "invalid height" << endl; else { ... } if (!(cin >> triHeight)) cerr << "invalid height" << endl; else { ... }

A right way close to yours using loops for all is (supposing you want a pyramid) :

#include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1) {
      for (int s = triHeight - h; s != 0; s -= 1)
        cout << ' ';
      for (int z = 2*(h-1)+1; z >0; z -=1)
        cout << '0';
      cout << endl;
    }
  }
}

Compilation and execution :

/tmp % g++ -pedantic -Wextra -Wall c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   000
  00000
 0000000
000000000
/tmp % 

If you want half a pyramid :

 #include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1) {
      for (int s = triHeight - h; s != 0; s -= 1)
        cout << ' ';
      for (int z = 0; z < h; z += 1)
        cout << '0';
      cout << endl;
    }
  }
}

Compilation and execution :

/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   00
  000
 0000
00000
/tmp % 

You can also do not make the two internal loops by yourself :

#include <iostream>

using namespace std; 

int main()
{
  int triHeight;

  cout << "Enter the triangle height: " << endl;
  if (!(cin >> triHeight))
    cerr << "invalid height" << endl;
  else {
    for (int h = 1; h <= triHeight; h += 1)
      cout << string(triHeight - h, ' ') << string(h, '0') << endl;
  }
}

Compilation and execution :

/tmp % g++ -pedantic -Wall -Wextra c.cc
/tmp % ./a.out
Enter the triangle height: 
5
    0
   00
  000
 0000
00000
/tmp % 

that solution is shorter but creates temporary strings

The simplest way to do this is to use the built in features of the std::iostream :

#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    int triHeight;
    int c = 0;

    std::cout << "Enter the triangle height: \n";
    std::cin >> triHeight;

    std::cout << std::setfill(' ') << std::right;
    for (c = 0; c < triHeight; c++)
    {
        std::cout << std::setw(triHeight) << std::string(c+1,'0') << '\n';
    }
    return 0;
}

You can get back to a left aligned triangle by simply changing std::right to std::left .

The logic of your program is wrong. At first you are trying to output the symbol 'O'

    for (r = 0; r < c; r = r + 1)
    {
        cout << "0";
    }

and after that you are trying to output spaces

    for (k = 0; k <= c; k = k - 1)
    {
        cout << " ";
    }

Moreover this loop is invalid because the variable k is decremented starting from 0.

The program can be written using only one loop and standard i/o manipulators declared in the header <iomanip>

Here is a demonstrative program

#include <iostream>
#include <iomanip>

int main() 
{
    while ( true )
    {
        const char c = 'O';

        std::cout << "Enter the height of a triangle (0 - exit): ";

        int height = 0;

        if ( not ( std::cin >> height ) or ( height <= 0 ) ) break;

        std::cout << '\n';

        for ( int i = 0; i < height; i++ )
        {
            std::cout << std::setw( height - i ) << std::setfill( ' ' ) << c;
            std::cout << std::setw( i + 1 ) << std::setfill( c ) << '\n';
        }

        std::cout << '\n';
    }

    return 0;
}

The program output might look the following way

Enter the height of a triangle (0 - exit): 1

O

Enter the height of a triangle (0 - exit): 2

 O
OO

Enter the height of a triangle (0 - exit): 3

  O
 OO
OOO

Enter the height of a triangle (0 - exit): 4

   O
  OO
 OOO
OOOO

Enter the height of a triangle (0 - exit): 5

    O
   OO
  OOO
 OOOO
OOOOO

Enter the height of a triangle (0 - exit): 6

     O
    OO
   OOO
  OOOO
 OOOOO
OOOOOO

Enter the height of a triangle (0 - exit): 7

      O
     OO
    OOO
   OOOO
  OOOOO
 OOOOOO
OOOOOOO

Enter the height of a triangle (0 - exit): 8

       O
      OO
     OOO
    OOOO
   OOOOO
  OOOOOO
 OOOOOOO
OOOOOOOO

Enter the height of a triangle (0 - exit): 9

        O
       OO
      OOO
     OOOO
    OOOOO
   OOOOOO
  OOOOOOO
 OOOOOOOO
OOOOOOOOO

Enter the height of a triangle (0 - exit): 10

         O
        OO
       OOO
      OOOO
     OOOOO
    OOOOOO
   OOOOOOO
  OOOOOOOO
 OOOOOOOOO
OOOOOOOOOO

Enter the height of a triangle (0 - exit): 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM