Triangle Program

Feb 28, 2012 at 7:55pm
Hey guys, Im new to C++ and have a problem for class we are supposed to try. I need to have a triangle formed of characters that varies based on what the user enters as the height. Its supposed to look like this:

*
**
***
****
***
**
*

...with varying height as an input.
I was able to get a right triangle for the way up...
i.e.

5

*
**
***
****
*****

but I am not sure how to go about for the other half of the triangle. My professor wants us to use a second for loop but I dont really know what to use for variables and such. Heres what I have so far...

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

double size= 0;
double drawline;
int userChoice = 0, x=1, y=1;
char sign =0;

int main ()
{
while(true)
{
cout << "***************************************" << endl;
cout << "* 1 - Triangle *" << endl;
cout << "* 2 - Sin Curve *" << endl;
cout << "***************************************" << endl;
cout << "Enter a selection, please: " << endl;

cin >> userChoice;

if (userChoice == 0)
{
return 0;
}
else if (userChoice == 1)
{
cout << "Please enter the character to be used: ";
cin >> sign;
cout << "Enter a size, please: ";
cin >> size;
void drawline (int size, char sign);
{
for (int x=1; x <= size; x++)
{
string str(x, sign);
cout << str << "\n";
}
while (true)
{
for (int x=5; x> size; x=x-1)
{
string str(sign, x);
cout << str << "\n";
}
}
}
}
}
}

Any help or criticisms is welcome as I am learning and know Im far from being good at this. Thanks
Feb 28, 2012 at 8:03pm
i kinda did a similar assignment in class
maybe this will help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
using namespace std;
int main()
{
  char size;
  int nsize;
  double count=0;
  cout<<"This program displays triangles based on the size entered.\n";
  cout<<"\n";
  do{
  cout<<"Please enter the size(s,m,l,x), or q to exit:"<<endl;
  cin>>size;
  while (size!='s' && size!='m' && size!='l' && size!='x' && size!='q'){
        cout<<"Wrong entry.Please enter the size again:"<<endl;
        cin>>size;}
  switch (size){
  case 's':
         nsize=3;
          break;
  case 'm':
         nsize=5;
         break;
  case 'l':
         nsize=7;
          break;
  case 'x':
         nsize=10;
          break;
  case 'q':
          cout<<"Program exiting...";
          nsize=0;
          break;}
  if (nsize!=0){
                
        for (int num=0;num<nsize;num++){
           cout<<"\n";
           count++;
           for (int wow=0;wow<count;wow++){
           cout<<"*";}}
        cout<<"\n";
        count=nsize;
        for (int num=0;num<=nsize;num++){
           cout<<"\n";
           count=count-1;
           for (int wow=0;wow<=count;wow++)
           cout<<"*";}
           count=0;
        cout<<"\n";        
        count=nsize+1;
        for (int num=0; num<nsize; num++) {
           cout<<"\n";
           count--;
           for (int wow=0; wow <nsize-count; wow++)
           cout<<" ";
           for (int chmo=0; chmo<count; chmo++)
               cout<<"*";}
        count=0;
        cout<<"\n";       
        for (int num=0; num<nsize; num++) {
           cout<<"\n";
           count++;
           for (int wow=0; wow < nsize-count; wow++)
           cout<<" ";
           for (int chmo=0; chmo<count; chmo++)
               cout<<"*";}
           cout<<"\n"<<"\n";
           count=0;}}
  while (size!='q');           
  return 0;
}

Feb 28, 2012 at 9:02pm
Just take the loop that you used to create the right triangle and create a new one that runs in reverse...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int triangle_size = 0;
  cout << "Enter size of triangle: ";
  cin >> triangle_size;
  cout << endl;
  for(int row_index = 0; row_index < triangle_size; ++row_index)
  {
    for(int col_index = 0; col_index < row_index; ++col_index)
    {
      cout << "*";
    }
    cout << endl;
  }
  for(int row_index = triangle_size; row_index; --row_index)
  {
    for(int col_index = 0; col_index < row_index; ++col_index)
    {
      cout << "*";
    }
    cout << endl;
  }
Feb 28, 2012 at 11:01pm
Thanks for the responses. Im a little closer now. This may be a dumb question, but how do you create a loop that runs in reverse? I have this....


else if (userChoice == 1)
{
cout << "Please enter the character to be used: ";
cin >> sign;
cout << "Enter a size, please: ";
cin >> size;
void drawline (int size, char sign);
{
for (int x=1; x <= size; x++)
{
string str(x, sign);
cout << str << "\n";
}
for (int x=1; x<size; x++)
{
string str(x, sign);
cout << str << "\n";
}
}
...and that displays

*
**
***
****
*
**
***

so it just counts back up to what was entered.

Thanks again guys!
Topic archived. No new replies allowed.