fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5.  
  6. int main()
  7. {
  8. unsigned num_lines;
  9. std::cout << "Enter the number of lines.\n> ";
  10. std::cin >> num_lines;
  11.  
  12. const unsigned width = 79;
  13. std::vector<std::string> lines(num_lines); // creates a vector of num_lines default constructed strings.
  14.  
  15. for (unsigned i = 0; i < lines.size(); ++i)
  16. lines[i] = std::string((width / 2 ) - (i * 2 + 1)/2, ' ') + std::string(i * 2 + 1, '*');
  17.  
  18. std::cout << '\n' ;
  19. for (unsigned i = 0; i < lines.size(); ++i)
  20. std::cout << lines[i] << '\n';
  21. }
Success #stdin #stdout 0s 3480KB
stdin
20
stdout
Enter the number of lines.
> 
                                       *
                                      ***
                                     *****
                                    *******
                                   *********
                                  ***********
                                 *************
                                ***************
                               *****************
                              *******************
                             *********************
                            ***********************
                           *************************
                          ***************************
                         *****************************
                        *******************************
                       *********************************
                      ***********************************
                     *************************************
                    ***************************************