For loop help!

Hey guys!

Having a bit of trouble trying to go about printing with for loops.

What I'm aiming for is the following...

----/----\
---/------\
--/--------\
-/----------\

Although all I can achieve so far is...

----/----\
----/----\
----/----\

The code I've put together is as follows...
(size being a user integer input)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i = 0; i < size; i++) //does (size) amount of ROWS
	{
	
		//Top line
		for (int i = 0; i < size; i++) //does (size) amount of dashes
		{
			cout << "-";
		}

		cout << "/";

		for (int i = 0; i < size; i++)
		{
			cout << "-";
		}

		cout << "\\" << endl;

	}


So what I need to achieve is having one less dash before the "/" and two extra dashes between the "/" and "\" - for every row downwards.

If anything isn't clear just let me know and i'll try and clarify anything!

Thanks in advance for any help guys!

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

int main()
{
	const int MY_SIZE = 10;
	int outerDashSize = MY_SIZE;
	int innerDashSize = MY_SIZE;
	
	for (int i = 0; i < MY_SIZE; i++) //does (size) amount of ROWS
	{

		
		for (int i = 0; i < outerDashSize; i++) //does (size) amount of dashes
		{
			cout << "-";
		}
		outerDashSize--;
		cout << "/";

		for (int i = 0; i < innerDashSize; i++)
		{
			cout << "-";
		}
		innerDashSize += 2;
		cout << "\\" << endl;

	}

	cin.ignore();
	return 0;
}
Thanks so much!

Quick question on that...

On line 26 does "+= 2" mean "++" twice?

And therefore "+= 3" would mean "++" 3 times?

I'm a beginner and I don't really understand what you're trying to do but why can't you just write it in a simple way like this

1
2
3
4
5
 int main (){
cout << " ----/----\\ " <<endl;
cout << " ---/------\\ " <<endl;
cout <<" --/---------\\"<<endl;
cout <<"-/-----------\\"<<endl;
On line 26 does "+= 2" mean "++" twice?
innerDashSize += 2; is a shorter way to write innerDashSize = innerDashSize + 2;

And therefore "+= 3" would mean "++" 3 times?

yes innerDashSize += 3; would mean add 3 to innerDashSize.
@veektorh

because the number of lines is user-defined ('size')
Awesome thanks so much Yanson!

@veektorh yeah you could do it like that but I need to keep the same pattern for a user entered size. Your method would work, but only for a single size (4 in the example). If the user wanted the same shape but across 5 lines it wouldnt work as we have only told the program what to do for 4! By using for loops we can get the program to do a certain amount of iterations of a task, meaning we can scale with the users input size.

I hope that helps!
Topic archived. No new replies allowed.