Star rhombus with while loop

I just made ​​a simple program. I need a little help to make this a perfect.

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
#include <iostream>
using namespace std;
int main()
{
int a,b,c,v,i,j,k,n;
cout<<"Triangle 1 :";
cin>>v;
cout<<endl;
cout<<"Triangle 2 :";
cin>>n;
cout<<endl;
for (i=n;i>=0;i--)
{
for(j=i;j>=0;j--)
{
cout<<" ";
}
for(k=0;k<=n-i;k++)
{
cout<<"*";
}
for(k=0;k<n-i;k++)
{
cout<<"*";
}
cout<<endl;
}
for(a=0;a<=v;a++)
{
for(b=0;b<=a;b++)
{
cout<<" ";
}
for(c=0;c>=(a-v);c--)
{
cout<<"*";
}
for(c=0;c<(v-a);c++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}


program is without color. I need help to create a different color in each row.
or any other program ideas? Star like a rhombus with while loop with different colour in each row.help this newbie.
Last edited on
Your code is hard to read without indentation.
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
#include <iostream>

using namespace std;

int main() {
    int a,b,c,v,i,j,k,n;
    cout<<"Triangle 1 :";
    cin>>v;
    cout<<endl;
    cout<<"Triangle 2 :";
    cin>>n;
    cout<<endl;
    for (i=n; i>=0; i--) {
        for(j=i; j>=0; j--)
            cout<<" ";
        for(k=0; k<=n-i; k++)
            cout<<"*";
        for(k=0; k<n-i; k++)
            cout<<"*";
        cout<<endl;
    }
    for(a=0; a<=v; a++) {
        for(b=0; b<=a; b++)
            cout<<" ";
        for(c=0; c>=(a-v); c--)
            cout<<"*";
        for(c=0; c<(v-a); c++)
            cout<<"*";
        cout<<endl;
    }
    return 0;
}
Triangle 1: 2

Triangle 2: 2

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


Now you want to know if you can make each row a different color? Console programs aren't designed to be "pretty" but that isn't to say certain things can't help. As far as I know, console colors can be changed, but only for the entire code, for example, you can change the background or foreground, but it applies to the entire console. Seperate colors do not exist in console programming.
Here is a way to get each row a different color. Right now, it looks best with a black background, otherwise, you get a band of color up to the asterisk, in the background colors.
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
// Star Rhombus.cpp : main project file.

#include <iostream>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);



int main() {
    int a,b,c,v,i,j,k,n;
    cout<<"Triangle 1 :";
    cin>>v;
    cout<<endl;
    cout<<"Triangle 2 :";
    cin>>n;
    cout<<endl;
    for (i=n; i>=0; i--) 
	{
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),2+i );
		// Experiment with the colors, from 0 to 255
		for(j=i; j>=0; j--)
            cout<<" ";
        for(k=0; k<=n-i; k++)
            cout<<"*";
        for(k=0; k<n-i; k++)
            cout<<"*";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15 );
		// Sets it to black background, white text
        cout<<endl;
    }
    for(a=0; a<=v; a++) 
	{
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(2+a) );
		for(b=0; b<=a; b++)
            cout<<" ";
        for(c=0; c>=(a-v); c--)
            cout<<"*";
        for(c=0; c<(v-a); c++)
            cout<<"*";
        cout<<endl;
    }
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15 );
	// Sets it back to black background, white text
    return 0;
}
Last edited on
Wow, I'm in complete awe, I was unaware this was even possible! After as many times as I tried in the last 8 or so years...just wow, thanks a lot.
@Volatile Pulse

I, too, enjoy learning things that I didn't know were possible. Mostly, I learn from this site. I'm glad to finally be able to show others something new. So, you're very welcome.
thanks for the advice and assistance. very helpful.
I need help once again. what if the program above will be converted into "while loop" with different colour in each row.
once again thanks for any help and suggestions that helped.
@Helpmeee

However you choose you have the program run. Just insert SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(176+a) ); before the cout of the asterisk, any text, or symbol you desire, and the color will be changed for the item printed. I changed the location of the SetConsoleTextAttribute(GetStdHandle() so that the blank spaces are bypassed, and only the astericks are printed in colors. Change the numbers (0 to 255), to experiment with other colors.
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
// Star Rhombus.cpp : main project file.

#include <iostream>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);



int main()
{
    int a,b,c,v,i,j,k,n;
    cout<<"Triangle 1 :";
    cin>>v;
    cout<<endl;
    cout<<"Triangle 2 :";
    cin>>n;
    cout<<endl;
    for (i=n; i>=0; i--) 
	{
       
		// Experiment with the colors, from 0 to 255
		for(j=i; j>=0; j--)
            cout<<" ";
         SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),176+i );
		 // Just insert the above, BEFORE you cout the asterisks
		 for(k=0; k<=n-i; k++)
            cout<<"*";
        for(k=0; k<n-i; k++)
            cout<<"*";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15 );
		// Sets it to black background, white text
        cout<<endl;
    }
    for(a=0; a<=v; a++) 
	{
        for(b=0; b<=a; b++)
            cout<<" ";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),(176+a) );
        for(c=0; c>=(a-v); c--)
            cout<<"*";
        for(c=0; c<(v-a); c++)
            cout<<"*";
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15 );
               // Sets it back to black background, white text
        cout<<endl;
    }
	
    return 0;
}
Last edited on
@whitenite1
thanks for the great help learning.

I have the same problem again on this program (a different color in each row). give me a clue to complete this program.

I've tried many times as you have previously taught. there will be the same color. when I want a different color in each row.

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

    int main()  
    {  
        int i, j, k,a;
		a=9;		
          
        for (i=0; i<=a; i+=2)  
        {  
           for(j=0; j<a-i; j+=2)  
           {  
              cout<<" ";  
           }  
           for(k=0; k<=i; k++)  
           {  
              cout<<"*";
           }  
           cout<<endl;  
        }  
        a=a-2;  
        for(i=0; i<a; i+=2)  
        {  
           for(j=0; j<=i+2; j+=2)  
           {  
              cout<<" ";  
           }  
           for(k=1; k<=a-i;k++)  
           {  
              cout<<"*";  
           }  
           cout<<endl;  
        }  
        return 0;  
   }	
Last edited on
The command would go just before cout << "*"; and the same command, but with a 15 for color, which is a black background and white text, after the cout.
Topic archived. No new replies allowed.