Making a pyramid program

I want the output to look something like this:

    Enter a value to represent the base of a triangle shape (not to exceed 80): 11    
    Enter the character to be used to generate the triangle shape (for eg., #, * $): *
     *
    ***
   *****
  *******
 *********
***********Do you want to quit the program? (type n for no or q to quit): q


My incorrect source code to attempt to acheive this exact output so far is:

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
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int stars=1;
int baseNumberStars;
char theSymbol;
string spaces = " ";

cout << "Enter a value to represent the base of a triangle shape (not to exceed 80): ";
cin >> baseNumberStars;
cout << "Enter the character to be used to generate the triangle shape (for eg., #, * $): ";
cin >> theSymbol;
if (baseNumberStars>80)
{
    cout << "Please enter a number 1-80 for triangle's base.\n";
    system("PAUSE");
    return(1);
}

if((baseNumberStars>0) || (baseNumberStars<=80))
{

for(int line=-1; line < baseNumberStars; line++)
{
for(int s=(baseNumberStars-1); s < stars; s++)
cout <<theSymbol;
stars+=2;
cout << endl;
}
system("PAUSE");
return(0);
}
}


I am very new to loops and am learning them for the first time so bear with me...
The things I need to do left for my program are:

1. I need to get it so my triangle is no longer a right triangle.
2. I need to make it so at the end of the program when "n" is inputted it repeats the program.

Also, I am confused why there is a large space between my triangle and the words above it.

I may not respond for a few hours or so after this. Your guys help appreciated, thanks again.
Last edited on
OP wrote:
2. I need to make it so at the end of the program when "n" is inputted it repeats the program.

Then read the input in a char(or any other thing), and break; the loop if q, do nothing if n, --optional-- or say something like "Wrong input" if any other character.

Working on the code.

Thanks,
Aceix.
At last solution found!--at least for the second question.
The problem is that, the tip is not pointed, and it also doubles the base of the triangle.--Maybe someone might try to help "us" out?

Code:

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
#include <iostream>
#include <cstdlib>

using namespace std;

void print_triangle(int,char);

int main()
{
    int iTriangleBase=0;
    char cTriangleStyle='*';
    char cExitCode;

    do
    {
        cExitCode=0;
        cout<<"Enter number for the base of the triangle(Note**Number would be doubled!**): ";
        cin>>iTriangleBase;
        cout<<"Enter style for triangle block: ";
        cin>>cTriangleStyle;
        print_triangle(iTriangleBase,cTriangleStyle);
        while(cExitCode!='q'&&cExitCode!='n')
        {
            cout<<"\n\nDo you want to quit(q) or stay(n): ";
            cin>>cExitCode;
            if(cExitCode!='n'&&cExitCode!='q')
                cout<<"\nWrong Input!!!";
        }
    }while(cExitCode!='q');

    return 0;
}

void print_triangle(int iBase,char cStyle)
{
    static int iTop=0;
    int iSpaces=iBase;
    int iTemp1,iTemp2;

    for(int i=1;iSpaces;iSpaces--,i++)
    {
        for(iTemp1=iSpaces;iTemp1;iTemp1--)
        {
            cout<<' ';
        }
        for(iTemp2=0;iTemp2!=i;iTemp2++)
        {
            cout<<cStyle;
            cout<<cStyle;
        }
        cout<<endl;
    }
}


Thanks,
Aceix.
Topic archived. No new replies allowed.