#include "stdafx.h" #include <iostream> #include <sstream> #include <string> using namespace std; int main() { // initialize variables string str; int num; int count = 1; bool sur = true; // user input cout << "Enter how many stars: "; getline(cin, str); stringstream(str) >> num; // test for null if (str.length() < 1) { num = 0; }; // make sure number is positive num = abs(num); cout << '\n'; if (num) { while (count != 0) { for (int i = 1; i <= count; i++) { cout << char(42); } cout << "\n"; if (count == num) { sur = false; } if (sur == true) { count++; } else { count--; } } } else { cout << "Invalid number!"; } // wait // getchar(); system("pause"); // for visual studio return 0; }
Output
Notes
#include "stdafx.h" #include <iostream> using namespace std; int main() { // input cout << "Enter depth of pyramid: \n"; int n; cin >> n; cout << "\n"; // ascending for (int i = 1; i <= n; i++) { for (int j = 0; j <i; j++) cout << "*"; cout << endl; } // descending for (int i = n - 1; i >0; i--) { for (int j = i; j > 0; j--) cout << "*"; cout << endl; } // wait system("pause"); return 0; }