473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Simple Triangle using For Loops: C++ Beginner

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
6 13330
jackj
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
3,652 Expert 2GB
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
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
3,652 Expert 2GB
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
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
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

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

Similar topics

0
by: kbloom503 | last post by:
Hi. I am required to take a VB .net 2005 programming course for my business major so have no clue what I am doing. I have a final homework assignment that needs to include a command control that will...
2
Baka otaku
by: Baka otaku | last post by:
I need to display the following triangle ~ 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 1 2 1 2 3 1 2 3 4
4
by: janice3 | last post by:
hi.I am a new member in this site. today my question is how to make a diamond shape using loops? the shape is like: * * * * * * * * * * * * * *...
1
by: vykkilynn | last post by:
My homework this week involved using the for statement to create loops that would display triangles. The only output we're allowed to use are forms of the print statements... i.e. print(),...
1
by: psbasha | last post by:
Hi, Is it possible to create a simple viewer using Tkinter Modueles?. If not ,is it possible thru Python and OpenGL or anyother ways of creating a simple viewer?. I would like to have the...
5
by: SM | last post by:
Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD>...
4
by: sanzi4 | last post by:
how to write a c program that prints a triangle of a given size using an array whose elements represent the number of stars(*)forming the triangle. for example if the size is 5 output wll be *...
4
by: milk242 | last post by:
I am trying to figure out how to print a triangle in the shape of this depending on the user input. If they input 4 the max height would be 4. * ** *** **** *** ** * I have no idea how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.