400,697 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 400,697 IT Pros & Developers. It's quick & easy.

Simple Triangle using For Loops: C++ Beginner

jackj
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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     //    int numSpaces;
  7.     //int numSpacesNeeded;    //counts spaces from edge to begin line on that row
  8.     int numberLines;            //user entered total number of lines for triangle
  9.     char symbol;            //user entered symbol to be used
  10.     char userprompt;        //user enters prompt y or no
  11.     int lineCounter;        //counter to determine what line is being drawn
  12.     int numSymbols;            //counter to determine how many symbols drawn
  13.     int numSymbolsNeeded = 1;    //counter to determine the total number of symbols
  14.                                 //to be drawn on that line
  15.  
  16.     cout << "Welcome to the triangle comploser. Just enter a symbol of your choice from the keyboard." << endl << endl;
  17.     cout << "Then enter how many lines (how large) you would like the triangle should be. Your limit is 40." <<endl <<endl;
  18.  
  19.  
  20.     cout << "Do you wish to continue? ( y = yes, n = no):"; //using && will require a response of y or n. user cannot continue until y or n entered.
  21.  
  22.     cin >> userprompt;
  23.  
  24.     userprompt = tolower(userprompt);  //tolower means to test for lowercase only (toupper)
  25.  
  26.     while (userprompt != 'y' && userprompt != 'n')
  27.     {
  28.         cout << "Invalid input, y = yes, n = no: ";
  29.         cin.sync();
  30.         cin.clear();
  31.         cin >> userprompt;
  32.     }
  33.         if (userprompt == 'n')
  34.  
  35.         return 0; //program will end here if answer is no
  36.  
  37.     while (userprompt == 'y')
  38.  
  39.     {
  40.         system("cls");  //clear screen
  41.  
  42.         cout << "How many lines do you want the triangle to be:";
  43.  
  44.         cin.sync();
  45.         cin.clear();
  46.         cin >> numberLines;  //cin line ignores enter key if no data entered
  47.  
  48.  
  49.     if (!cin || numberLines >40 || numberLines<2)
  50.     {
  51.  
  52.         if (!cin)
  53.         {
  54.             cout << "Invalid response, try again. Please enter a positive integer.";
  55.         }
  56.         else if(numberLines >40)
  57.         {
  58.             cout << "You must enter a number less than 40"; // test for invalid input - negative numbers, character data, lines over 40
  59.         }
  60.         else if(numberLines <2)
  61.         {
  62.             cout << "You must enter a number of 2 or greater to create a triangle.";
  63.         }
  64.         cin.sync();
  65.         cin.clear();
  66.         cin >> numberLines;
  67.     }
  68.  
  69.  
  70.     cout << "Thank you. Now enter the symbol to be used to draw the triangle: ";
  71.     cin.sync();
  72.     cin.get (symbol);
  73.  
  74.     for (lineCounter = 1; lineCounter <= numberLines; lineCounter++)
  75.     {
  76.         for (numSymbols = 0; numSymbols < numSymbolsNeeded; numSymbols++)
  77.         {
  78.             cout << symbol;        //add 1 symbol at a time on the same line
  79.                                 //until the number of symbols written is
  80.                                 //equal to the number of symbols neeeded
  81.                                 //for this line.
  82.         }
  83.         cout << endl;
  84.         numSymbols = 0;
  85.         numSymbolsNeeded++;
  86.  
  87.     }
  88. return 0;
  89.  
  90.     }
Nov 13 '07 #1
Share this Question
Share on Google+
6 Replies


jackj
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
  1. for (lineCounter = 1; lineCounter <= numberLines; lineCounter++)
  2.     {
  3.         for (numSymbols = 0; numSymbols < numSymbolsNeeded; numSymbols++)
  4.         {
  5.             cout << symbol;        //add 1 symbol at a time on the same line
  6.                                 //until the number of symbols written is
  7.                                 //equal to the number of symbols neeeded
  8.                                 //for this line.
  9.         }
  10.         cout << endl;
  11.         numSymbols = 0;
  12.         numSymbolsNeeded++;
  13.     }
Nov 13 '07 #2

Ganon11
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

jackj
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

Ganon11
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
  1.   *
  2.   **
  3.  ***
  4.  ****
  5. *****
In order to get it perfectly centered, you may have to print spaces in between the symbols:

Expand|Select|Wrap|Line Numbers
  1.     *
  2.    * *
  3.   * * *
  4.  * * * *
  5. * * * * *
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

jackj
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 <<endl;


will try again tomorrow ... thnx 4 ur patience.
Nov 13 '07 #6

jackj
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<<setw(width);


while(b > 0) // This while loop will continue to output the desired symbol to the current line until it is equal to 1
{
cout<<letter;
b--;
}


cout<<endl;

b = (a * 2) - 1;

width--;

b = b + 2;

a++;

counter++;
}

cout<<endl<<endl; // endl

b = 1;
counter = 0; }
cout << endl;

cout << "Do you wish to continue? (y = yes, n = no): ";
cin >> userprompt;
Nov 18 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.