Write out a triangle

Pages: 12
Hello!
I hope there isn't a thread about this already, if there is I blame my real bad skills on searching.. :P

Anyhow, I need to train my knowledge in c++ and decided to make a program that makes a triangle (90 degrees bottom left corner.. Dont know the english word for it.. equilateral?) from what the user types.

designate sign: +
designate height: 5

+
++
+++
++++
+++++

Thats what I wanna do..
My code right now is:

1
2
3
4
5
6
7
8
9
char sign;
    int height;
    cout << "Designate sign: ";
    cin >> sign;
    cout << "Designate height: ";
    cin >> height;
    for (height < 50; height--;) {
        cout << height * sign << endl;
         }


I know it's not done, but I have no clue at all of how I should continue...
Thanks for help! =)
Your loop should look something like this: for ( int i = 0; i < height; i++ )
Then you should have another loop inside that or use setw / setfill http://www.cplusplus.com/reference/iostream/manipulators/setfill/
Thanks!

Though, I don't understand how I shall use setw / setfill, and I don't get it from the link you sent either..
Pretend that I'm a child, and explain to me ;) Haha
#include <iostream>
using namespace std;
int main (){
int i,j,k,a;
char mat[5][5];
for (i=0;i<5;i++){
for(j=0;j<5;j++){
k=(i-j);
do{
if (k>=0){
mat[i][j]='+';
cout<<mat[i][j];}
else {
cout<<" ";}}
while (a=0);}
cout<<"\n";}
system("PAUSE");
return 0;}
---------------------------------------------------------
copy this and run it...
---------------------------------------------------------
Last edited on
Yeah, that is working alright. But my goal was to let the user designate wich kind of sign (for example: + * / A B 1.. etc. etc.), and then the height of the triangle.

So that it could look like:

1
11
111

*
**

/
///
////
/////

And so on...

If someone could explain to me how to do this, or give me a code to it so that I could analyze it and learn, I'd be overjoyed :)

Thanks for the time you put in though TheNo1, much appriciated! :)
Np! :)

working on what you asking 4 ..
Done :D
--------------------------
#include <iostream>
using namespace std;
int main (){
int i,j,k,a,R,C,c;
char mat[99][99];
cout<<"Enter ROWS"<<endl;
cin>>R;
cout<<"Enter COLS"<<endl;
cin>>C;
cout<<"Enter char"<<endl;
cin>>c;
for(i=0;i<R;i++){
for(j=0;j<C;j++){
k=(i-j);
do{
if(k>=0){
mat[i][j]=c;
cout<<mat[i][j];}
else{
cout<<" ";}}
while(a=0);}
cout<<"\n";}
system("PAUSE");
return 0;}
--------------------------------------------------------------
// (c) TheNo1 ; Lev.buchel@gmail.com :)
-----------------------------------------------------
What the...? Don't do other people's homework!

EDIT: And your style is god-awful.
Last edited on
Thanks for your appreciation.
I'll try hard to improve my "STYLE"..
So that you like it.
Helios its christmas be nice! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
    char sign;
    int row = 0;
    
    std::cout << "Designate sign: ";
    std::cin >> sign;
    std::cin.ignore();
    
    std::cout << "Designate No. of rows: ";
    std::cin >> row;
    std::cin.ignore();
    
    for(int i=1; i<=row; i++) {
      std::string str(i, sign);
      std::cout << str << "\n";
    }
        
    std::cin.get();
    return 0;
}


Look at the constructor for a string object: http://www.cplusplus.com/reference/string/string/string/
Wow :) Mcleano, I really like it.
It's more simple and effective.
But I think you should add the way we see the triangle (for example the +).
Right now it's showing the triangular shape, by the last number that the user enters.
It's not such a big problem .. The program would be perfect in this manner.
Merry Christmas.... :D
Last edited on
I don't understand what it's missing? The user can specify both the sign( a '+' for example) and the number of rows.
sry.. My mistake.
@Helios
Homework? :P
Nah, I'm trying to learn C++, not make homework..

@Mcleano
HUGE thanks for the code, now I can analyze and (hopefully xD) learn something new :)
Thanks again! :)
here is a simple code to accomplish the character triangle.



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

#include <iostream>
using namespace std;



int main()

{
     char a;
     int b,c,d;




     cout << "designate sign";

     cin >> a;

     cout << "designate height";

     cin >> b;





     for (c=1;c<=b;c++)
     
          {
               {

                    for(d=1;d<=c;d++) cout << a;
               }


               cout << "\n";
          }

     return 0;
}




input a'+', b'10'.
output:

+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++



Thanks BettyBoopTS!
That code is a bit easier, I am more on that level than on mcleanos, even though that was good too.

Though I have a question:

To what good is the 37:th line?
(cout << "\n"; )

I mean.. All it does is to cout a new line? And if that line is removed it's not working anymore..
Why is it not possible to only use a << endl; in the "for-section" ?

Looks bad with a cout-line like that, but is it the only solution in this code?
That prints the newline after the inner loop has drawn the row of characters
Why is it not possible to only use a << endl; in the "for-section" ?
Who said it's not?
I'm just a beginner myself, and honestly, I may sound like an idiot, but I havnt come across the 'endl' command in the c++ beginners manual i am studying, yet. but,, why does that look bad? i dont understand.
@Helios.
True, none. Though I tried to use it and then nothing worked, and yes I did use it at different locations. (can't remember where though)
Therefore I, probably I was wrong, guessed that it's not possible.

@Betty
Bad is probably wrong choice of words, but in my opinion it looks... My english wordbank is too small right now, so bad is the only word I can figure out..
To only cout out a "\n" looks in my opinon not good, if someone would look at the code. To use a << endl; is "nicer".


Anyhow..
The cout-line is a part of... Let's see.. The loop on line 28, am I right?

That would explain why my <<endl; didn't work since I tried to insert it in the second loop.... :rolleyes:
Pages: 12