417,831 Members | 942 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 417,831 IT Pros & Developers. It's quick & easy.

# Simple Triangle using For Loops: C++ Beginner

 P: 5 Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a triangle that aligns to the left of the screen, but it loops and disappears immediately. So, my problem is getting it to stay on the screen and then ask the user if they want to create another one AND to make it print the first symbol in the center of the screen and build the triangle from there. Thnx in advance. _________________________________________ Expand|Select|Wrap|Line Numbers #include  using namespace std;   int main() {     //    int numSpaces;     //int numSpacesNeeded;    //counts spaces from edge to begin line on that row     int numberLines;            //user entered total number of lines for triangle     char symbol;            //user entered symbol to be used     char userprompt;        //user enters prompt y or no     int lineCounter;        //counter to determine what line is being drawn     int numSymbols;            //counter to determine how many symbols drawn     int numSymbolsNeeded = 1;    //counter to determine the total number of symbols                                 //to be drawn on that line       cout << "Welcome to the triangle comploser. Just enter a symbol of your choice from the keyboard." << endl << endl;     cout << "Then enter how many lines (how large) you would like the triangle should be. Your limit is 40." <> userprompt;       userprompt = tolower(userprompt);  //tolower means to test for lowercase only (toupper)       while (userprompt != 'y' && userprompt != 'n')     {         cout << "Invalid input, y = yes, n = no: ";         cin.sync();         cin.clear();         cin >> userprompt;     }         if (userprompt == 'n')           return 0; //program will end here if answer is no       while (userprompt == 'y')       {         system("cls");  //clear screen           cout << "How many lines do you want the triangle to be:";           cin.sync();         cin.clear();         cin >> numberLines;  //cin line ignores enter key if no data entered         if (!cin || numberLines >40 || numberLines<2)     {           if (!cin)         {             cout << "Invalid response, try again. Please enter a positive integer.";         }         else if(numberLines >40)         {             cout << "You must enter a number less than 40"; // test for invalid input - negative numbers, character data, lines over 40         }         else if(numberLines <2)         {             cout << "You must enter a number of 2 or greater to create a triangle.";         }         cin.sync();         cin.clear();         cin >> numberLines;     }         cout << "Thank you. Now enter the symbol to be used to draw the triangle: ";     cin.sync();     cin.get (symbol);       for (lineCounter = 1; lineCounter <= numberLines; lineCounter++)     {         for (numSymbols = 0; numSymbols < numSymbolsNeeded; numSymbols++)         {             cout << symbol;        //add 1 symbol at a time on the same line                                 //until the number of symbols written is                                 //equal to the number of symbols neeeded                                 //for this line.         }         cout << endl;         numSymbols = 0;         numSymbolsNeeded++;       } return 0;       } Nov 13 '07 #1
6 Replies

 P: 5 k. i have fixed everything except the calculation for the triangle itself. i don't really understand how the code creates the triangle in the first place, so making it center on the page is not so easy now. the following snippet will create a triangle justified to the left. and if the triangle is, say 7 rows, then there will need to be 3 spaces counted over for line one to the center then a symbol, 2 for line 2, 1 for line 3... i guess. Expand|Select|Wrap|Line Numbers for (lineCounter = 1; lineCounter <= numberLines; lineCounter++)     {         for (numSymbols = 0; numSymbols < numSymbolsNeeded; numSymbols++)         {             cout << symbol;        //add 1 symbol at a time on the same line                                 //until the number of symbols written is                                 //equal to the number of symbols neeeded                                 //for this line.         }         cout << endl;         numSymbols = 0;         numSymbolsNeeded++;     } Nov 13 '07 #2

 Expert 2.5K+ P: 3,652 OK, so let's take your example of 7 rows. Then, at the bottom, you will need 0 spaces, then 7 symbols, then 0 spaces. Move one level up. You need 1 space, 6 symbols, 0 spaces (or 0, 6, 1, depending on your fancy). Move one level up. You need 1 space, 5 symbols, 1 space. Move one level up. You need 2 spaces, 4 symbols, 1 space (or 1, 4, 2). Move one level up. You need 2 spaces, 3 symbols, 2 spaces. Move one level up. You need 3 spaces, 2 symbols, 2 spaces (or 2, 2, 3). Move one level up. You need 3 spaces, 1 symbol, 3 spaces. Can you see a pattern between the numbers and, thus, form an algorithm? Nov 13 '07 #3

 P: 5 OK, so let's take your example of 7 rows. Then, at the bottom, you will need 0 spaces, then 7 symbols, then 0 spaces. Move one level up. You need 1 space, 6 symbols, 0 spaces (or 0, 6, 1, depending on your fancy). Move one level up. You need 1 space, 5 symbols, 1 space. Move one level up. You need 2 spaces, 4 symbols, 1 space (or 1, 4, 2). Move one level up. You need 2 spaces, 3 symbols, 2 spaces. Move one level up. You need 3 spaces, 2 symbols, 2 spaces (or 2, 2, 3). Move one level up. You need 3 spaces, 1 symbol, 3 spaces. Can you see a pattern between the numbers and, thus, form an algorithm? i see that for every OTHER line, one space must be added. i am totally clueless on how to begin writing such an equation. i had 2 lol at your reply, because you sound exactly like my instructor. my prob is that i am not making connections between how to write the code. all the ++, -- stuff is just beginning to make sense and i understand the basic concept of algorithms. i suspect the key lies in the spaces. the pseudo code for the existing would be if spaces are=0, then the num of spaces should be less than the num of spaces needed, so increase one at a time until it reaches that limit. That creates a triangle, left justified, so it is counting on right side... prints a symbol and then counts. i want to count spaces, print symbol, count spaces? Nov 13 '07 #4

 Expert 2.5K+ P: 3,652 OK, the pattern I saw that emerged was this: For a row n from the top: 1) Print (ROW_NUM - n) / 2 spaces 2) Print n symbols 3) Print (ROW_NUM - n) / 2 spaces Notice that, if n is odd, then (because of integer division) you will end up with 1 less space than necessary (Quick example: If n = 5, then n/2 == 2, and you will print 4 spaces total - but you will need 5 spaces total printed) - this is why you need to explicitly add another space somewhere if n is odd. Actually, you don't need to worry about the spaces afterward - you can simply print a newline character (endl) and move on. Note: This triangle will not be perfectly centered; it will look something like: Expand|Select|Wrap|Line Numbers   *   **  ***  **** ***** In order to get it perfectly centered, you may have to print spaces in between the symbols: Expand|Select|Wrap|Line Numbers     *    * *   * * *  * * * * * * * * * which changes the logic of the problem. I leave it up to you to find out how many spaces you need and where. Nov 13 '07 #5

 P: 5 i can't say that i have progressed greatly. i am now very tired and have only managed to create an empty triangle. thusly: rownumber = 0; while (rownumber < numberLines - 1) { col = 0; while (col < numberLines + rownumber) { ++col; if (col == numberLines - rownumber) cout << symbol; else { if (col == numberLines + rownumber) cout << symbol; else cout << ' '; } } cout << endl; ++rownumber; } // draw the base col = 0; while (col < numberLines * 2 - 1) { cout << symbol; ++col; } cout <

 P: 5 is it poss to change these while statements into For statements? I understand that the For statement combines three actions in one statement, where the while statement has them spread throughout. However, I don't see the exact connection on what to combine where... width = (base / 2) + 5 - counter; // This is how far into the center it should space until it starts outputting the symbol a = 1; while (width > 5) { width = (base / 2) + 5 - counter; // This is how far into the center it should space until it starts outputting the symbol cout< 0) // This while loop will continue to output the desired symbol to the current line until it is equal to 1 { cout<> userprompt; Nov 18 '07 #7