Drawing a Diagonal

I'm new to this. I want this to result in a diagonal line.

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

using std::cout; using std::endl; using std::cin;

int main() 
{
	cout << "Input size: ";
	int size;
	cin >> size;
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j == i; j++)
		{
			cout << "+";
		}
		cout << endl;
	}
	cout << endl;
}


The results are just:
Input size: 4
+

What I want:
Input size: 4
+
*+
**+
***+

Note: Pretend * are empty spaces
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using std::cout; using std::endl; using std::cin;

int main() 
{
	cout << "Input size: ";
	int size;
	cin >> size;
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < size; j++)
		{
			if (j != i)
				cout << " ";
			else
			cout << "+";
		}
		cout << endl;
	}

NVM I think I solved it.

Edit: Actually is there a way to do it with out the if else. By that I mean by having it output + and not have to use
" ".
Last edited on
Actually is there a way to do it with out the if-else
1
2
3
4
5
6
7
8
for (int i = 0; i < size; i++)
{
    // print i spaces
    for (int j = 0; j < i; ++j) 
        std::cout << ' ';
    // then print a + followed by a new-line:
    std::cout << "+\n";
}


You can't eliminate printing the spaces entirely, unless you choose to treat standard output like a terminal rather than an output stream and control it specifically. This can't be done in a standard way, and not at all if standard output was going to a file instead.
Last edited on
or

1
2
for (int i = 0; i < size; i++)
    std::cout << std::string(i,' ') << "+" << std::endl;
Last edited on
At cosmicInferno,

You had a correct solution except line12.

for (int j = 0; j == i; j++)

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

using std::cout; using std::endl; using std::cin;

int main() 
{
	cout << "Input size: ";
	int size;
	cin >> size;
	for (int i = 0; i < size; i++)
	{
		for (int j = 0; j < i; j++)
		{
			cout << " ";
		}
		cout << "+" << endl;
	}
	cout << endl;
}
Last edited on
Topic archived. No new replies allowed.