0

how can i create triangle with c++

#include <iostream> using namespace std; int Rectangle() { int sEdge,lEdge; cout << "short edge: "; cin >> sEdge; cout << "long edge: "; cin >> lEdge; while(lEdge>0) { for(int i=1;i<=sEdge;i++) { cout << '*'; } cout << endl; lEdge-=1; } } int RightTriangle() { int height; cout << "height: "; cin >> height; bool a; while(a) { for (int i=1;i<=height;i++) for(int j=0;j<i;j++) cout << '*'; } cout << endl; height--; } int Equilateraltriangle() { int a,b,height; cout << "height: "; cin >> height; a=height; b=height; while(a>0) for (int i=0;i<a;i++) { cout << " "; } for(int j=0;j<=height;j++) { cout << "*"; } b=b+2; a--; } int Triangle() { int option; cout << "1.Right Triangle\n2.Equilateral triangle\n"; cin >> option; switch(option){ case 1: RightTriangle(); break; case 2: Equilateraltriangle(); break; default: break; } } int main() { bool a=true; while(a){ int select,select2,yselect2; cout << "1.Rectangle\n2.Triangle"; cout << "\noption: "; cin >> select; switch (select){ case 1: Rectangle(); break; case 2: Triangle(); break; default: break; } cout << "1.New Operation\n2.Exit"; cout << "\noption: "; cin >> select2; switch(select2) { case 1: break; case 2: a=false; default: cout << "Incorrect Entry Please Try Again"; cout << "\noption: "; cin >> yselect2; // i want to use switch(select2) with that but doesnt work select2=yselect2; break; } } } this is my code but there are sth wrong with RightTriangle() and Equilateraltriangle() and also i wanted to use switch(select2) after wrong entry how can i fix this.

16th Mar 2017, 10:19 PM
Furkan Kabak
Furkan Kabak - avatar
6 Answers
+ 15
That feeling when you click that link and it's your code being shared :D
17th Mar 2017, 12:08 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
I suggest checking out code that has been put up on the code space. I found this one, didn't look at the details but you will get lots of ideas from other people's code. https://code.sololearn.com/c6Zmx28SiC4s/?ref=app
16th Mar 2017, 10:33 PM
Elric Hindy
Elric Hindy - avatar
+ 3
Hi, that's a great start to a program. I've made a few changes to make your triangle routines work and I'll include explanations as another post due to the limited character count on SoloLearn. Hopefully the explanations will make sense and let you understand what I've done. #include <iostream> using namespace std; void Rectangle() { int sEdge, lEdge; cout << "short edge: "; cin >> sEdge; cout << "long edge: "; cin >> lEdge; while (lEdge>0) { for (int i = 1; i <= sEdge; i++) { cout << '*'; } cout << endl; lEdge -= 1; } } void RightTriangle() { int height; cout << "height: "; cin >> height; for (int i = 1; i <= height; i++) { for (int j = 0; j < i; j++) { cout << '*'; } cout << endl; } } void Equilateraltriangle() { int a, b, height; cout << "height: "; cin >> height; a = height; b = 0; while (a > 0) { for (int i = a - 1; i > 0; i--) { cout << " "; } for (int j = 1; j <= 2*b-1; j++) { cout << "*"; } cout << endl; a--; b++; } } void Triangle() { int option; cout << "1.Right Triangle\n2.Equilateral triangle\n"; cin >> option; switch (option) { case 1: RightTriangle(); break; case 2: Equilateraltriangle(); break; default: break; } } int main() { bool a = true; while (a) { int select, select2, yselect2; cout << "1.Rectangle\n2.Triangle"; cout << "\noption: "; cin >> select; switch (select) { case 1: Rectangle(); break; case 2: Triangle(); break; default: break; } cout << "1.New Operation\n2.Exit"; cout << "\noption: "; cin >> select2; switch (select2) { case 1: break; case 2: a = false; break; default: cout << "Incorrect Entry Please Try Again"; cout << "\noption: "; cin >> yselect2; // i want to use switch(select2) with that but doesnt work select2 = yselect2; break; } } }
17th Mar 2017, 12:10 AM
Shane
+ 3
You made a good job of the coding and it was only a couple of small things that made the code not work. Here is the explanation of the changes I made in my earlier post. To start with I've changed all of your functions to void rather than int as they don't return any values (you only need int if they are going to pass back a result from the function). The right angle triangle didn't need the while (a) lines as the for loops did everything and you weren't changing the value of a at all. The equilateral triangle was almost correct but it needed a small tweak in the for loops to make sure that it wrote the correct number of spaces and stars for each line and needed to include an endl. Finally I put a break in your select 2 for case 2 so that it didn't keep saying "Incorrect entry". I hope this helps.
17th Mar 2017, 12:15 AM
Shane
0
Im in scholl right now i will try that at home ty for the helping i hope it works
17th Mar 2017, 5:03 AM
Furkan Kabak
Furkan Kabak - avatar
0
void Equilateraltriangle() { int a, b, height; cout << "height: "; cin >> height; a = height; b = 0; while (a > 0) { for (int i = a - 1; i > 0; i--) { cout << " "; } for (int j = 1; j <= 2*b-1; j++) { cout << "*"; } cout << endl; a--; b++; } } this code doesnt give me a triangle with 5 height.It gives me 4 height . and also i managed to fix my code like this void Equilateraltriangle() { int a,b,height; cout << "height: "; cin >> height; a=height; b=1; while(a>0){ for (int i=0;i<a;i++) { cout << " "; } for(int j=0;j<b;j++) { cout << "*"; } cout << endl; b=b+2; a--; } } i forgot {} in while loop :D thanks for good responses
17th Mar 2017, 12:04 PM
Furkan Kabak
Furkan Kabak - avatar