Problem 2



I am trying to write a code with for nested loops but the problem I have is
with alignment meaning it does not align properly. Does anyone understand

the sample output:
****** ///////////// ******
***** ///////////\\ *****
**** /////////\\\\ ****
*** //////\\\\\\\ ***
** ////\\\\\\\\\ **
* \\\\\\\\\\\\\ *


#include <iostream>
using namespace std;

int main ()
{
int i;
int j;
int k;
int n;

for (i = 0; i < 7; i++)
{
for (n = i; n < 6; n++)
{
cout << "*";
}
for(n = 0;n < i+2; n++){
cout<<" ";
}
cout<<" ";
for (j = i; j < 13;j++)
{
cout << "/";
}
if(i!=0)
{
for(k = 0; k<i+1;k++){
cout<< "\\";
}
}

cout<<" ";
for(n = 0; n < i+2; n++){
cout<<" ";
}
for (n = i; n < 6; n++)
{
cout << "*";
}

cout<<endl;
}

system("PAUSE");
return 0;
}
@Bill2345,
PLEASE USE CODE TAGS (the <> formatting button to the right), when posting code.

Along with the proper indenting, it makes it easier to read your code, and thus also easier to respond to your post.

Tutorials on how to use code tags:

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

I found the second link to be the most help.

Hint: You can hit "edit post", highlight your code and then press the <> formatting button. This will not automatically indent your code. That part is up to you.

I've found it's easiest to copy and paste pre-indented code directly from the IDE, that way it is already properly formatted.

You can use the "preview" button at the bottom to see how it looks.

Also, delete the system ("PAUSE");, it is unnecessary and it will cause compiler errors if you give your code to someone else.

Thanks!
max
Ok, here's what I got after reading over your code:

For one, you should initialize your for loop variables inside the for loop. Makes it easier to read because that's the standard way to do it.

Next, you should use a separate variable for each for loop. Don't use "n" in a bunch of successive for loops. That's just asking for trouble.

Here's your code, edited with comments and whitespace for better readability:
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
#include <iostream>

int main ()
{
	for (int i = 0; i < 7; i++)
	{
		for (int n = i; n < 6; n++)
		{
			std::cout << "*";
		}
		for (int m = 0; m < i+2; m++)
		{
			std::cout << " ";
		}
		
		std::cout << " ";
		
		for (int j = i; j < 13; j++)
		{
			std::cout << "/";
		}
		
		if (i != 0)
		{
			for (int k = 0; k < i+1; k++)
			{
				std::cout << "\\";
			} 
		}
		// Probably should put an else in here, for error checking

		std::cout << " ";
		
		for (int p = 0; p < i+2; p++)
		{
			std::cout << " ";
		}
		
		for (int q = i; q < 6; q++)
		{
			std::cout << "*";
		}

		std::cout << std::endl;
	}

	// unnecessary and will cause portability issues
	//system("PAUSE"); 
	
	// not needed but makes a good break point
	return 0;
}
Output:
******   /////////////   ******
*****    ////////////\\    *****
****     ///////////\\\     ****
***      //////////\\\\      ***
**       /////////\\\\\       **
*        ////////\\\\\\        *
         ///////\\\\\\\         

What output are you looking for, exactly?
This can be vastly simplified using iomanipulators. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>

int main()
{
	constexpr size_t rows(7);

	for (size_t i = 0; i < rows; ++i) {
		std::cout << std::setw(rows - i) << std::setfill('*') << '*';
		std::cout << std::setw(i + 2) << std::setfill(' ') << ' ';
		std::cout << std::setw(13 - i) << std::setfill('/') << '/';
		std::cout << std::setw(i + 1) << std::setfill('\\') << '\\';
		std::cout << std::setw(i + 3) << std::setfill(' ') << ' ';
		std::cout << std::setw(rows - i) << std::setfill('*') << '*';
		std::cout << '\n';
	}
}



*******  /////////////\   *******
******   ////////////\\    ******
*****    ///////////\\\     *****
****     //////////\\\\      ****
***      /////////\\\\\       ***
**       ////////\\\\\\        **
*        ///////\\\\\\\         *


or without using iomanip:

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

void fill(size_t no, char ch)
{
	for (size_t i = 0; i < no; ++i)
		std::cout << ch;
}

int main()
{
	constexpr size_t rows(7);

	for (size_t i = 0; i < rows; ++i) {
		fill(rows - i, '*');
		fill(i + 2, ' ');
		fill(13 - i, '/');
		fill(i + 1, '\\');
		fill(i + 3, ' ');
		fill(rows - i, '*');
		std::cout << '\n';
	}
}


Last edited on
Hey Max I was wondering if you can get the output same to seeplus.
I help with this
//////////////\
/////////////\\
////////////\\\
///////////\\\\
//////////\\\\\
/////////\\\\\\
////////\\\\\\\
//////\\\\\\\\\\
Plod your way through it bit by bit like this:
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
#include <iostream>

int main()
{
    int no_first_character{0};
    int no_second_character{0};

    for(int line_no = 0; line_no < 8; line_no ++ )
    {
        no_first_character = 13 - line_no;
        for(int i = 0; i < no_first_character; i++)
        {
            std::cout << '/';
        }

        no_second_character = line_no + 1;
        for(int i = 0; i < no_second_character; i++)
        {
            std::cout << '\\'; // <--
        }

        std::cout << '\n';
    }

    return 0;
}



/////////////\
////////////\\
///////////\\\
//////////\\\\
/////////\\\\\
////////\\\\\\
///////\\\\\\\
//////\\\\\\\\
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const int N = 7;
   string left  = string( N, '*' ) + string( N, ' ' );
   string middle = string( 2*N, '/' ) + string( N, '\\' );
   string right( left.rbegin(), left.rend() );
   for ( int i = 0; i < N; i++ ) cout << left.substr(i,N+1) << middle.substr(i+1,2*N) << right.substr(N-i-1,N+1) << '\n';
}


******* /////////////\ *******
******  ////////////\\  ******
*****   ///////////\\\   *****
****    //////////\\\\    ****
***     /////////\\\\\     ***
**      ////////\\\\\\      **
*       ///////\\\\\\\       *

And then there is a function driven way ...
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>

void print(char ch, int num)
{
    for(int i = 0; i < num; i++)
    std::cout << ch;
}

int main()
{
    for(int line_no = 0; line_no < 7; line_no++ )
    {
        print('*', 7 - line_no);
        print(' ', line_no + 1);
        print('/', 13 - line_no);
        print('\\', line_no + 1);
        print(' ', line_no + 1);
        print('*', 7 - line_no);

        std::cout << '\n';
    }

    return 0;
}



******* /////////////\ *******
******  ////////////\\  ******
*****   ///////////\\\   *****
****    //////////\\\\    ****
***     /////////\\\\\     ***
**      ////////\\\\\\      **
*       ///////\\\\\\\       *
Program ended with exit code: 0
@Bill2345,
Out of curiosity, what is this for? Just experimenting, homework, TUI...?
Topic archived. No new replies allowed.