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.
_________________________________________ -
#include <iostream>
-
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." <<endl <<endl;
-
-
-
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.
-
-
cin >> 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;
-
-
}
6 13330
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. -
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++;
-
}
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?
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?
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:
In order to get it perfectly centered, you may have to print spaces in between the symbols: -
*
-
* *
-
* * *
-
* * * *
-
* * * * *
which changes the logic of the problem. I leave it up to you to find out how many spaces you need and where.
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.
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;
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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:
*
* * *
* * * * *
* * * * *...
|
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(),...
|
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...
|
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>...
|
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
*...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |