Setting spaces with loops?

Hello there, I have a problem, I have been trying really hard to write the following program for my Programming class:

Write a program using a single ‘for’ loop and have a person walk down the stairs.
You only need to make the man go down 5 stairs. It should look like this:
* o
* /|\
* / \
******
.....* o
.....* /|\
.....* / \
..... ******
.......... * o
.......... * /|\
.......... * / \
.......... ******
............... * o
............... * /|\
............... * / \
............... ******
.................... * o
.................... * /|\
.................... * / \
.................... ******
Hint: Code the person block first.. Next, In time the for loops, how many spaces to print before person at each step…

I have been trying, but all I can do is put the person there and make the loop print out the person 5 times and I can't understand how to put the spaces before the man so that he moves to the right. I am only allowed to code the person ONCE in my program, the point of the assignment is for me to better understand how to use for loops but I am stuck, here is the code I have...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{	
	for (int steps = 0; steps <= 5; steps++)
	{
		cout << "*O" << endl;
		cout << "/|\\" << endl;
		cout << "/\\" << endl;
		cout << "******" << endl;
		cout << endl;
		
	}
	system("pause");
	return 0;


Oh, and I am only allowed to use a SINGLE for loop, so no nested for loops, although I would also like to learn how to do it that way if you can...
Can someone help me? Thank you



EDIT:
Okay so I kinda worked it out... I am using the setw () operator, and I used steps in the parentheses like this:
1
2
3
4
5
6
7
8
9
10

for (int steps = 0; steps <= 5; steps++)
	{		
		cout << setw(steps * 5) << "*O" << endl;
		cout << setw(steps * 5) << "/|\\" << endl;
		cout << setw(steps * 5) << " /\\" << endl;
		cout << setw(steps * 5) << "******" << endl;
		cout << endl;
		
	}


the problem now is that it prints it out normally once and then the man walks down the stairs normally, but his body starts to get cricked and his head comes off... not by a lot, just slightly, but its still annoying..
Any suggestions please?
Last edited on
Do you mean something like this:

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

using namespace std;

int main(int argc, char* argv[])
{	
	for (int steps = 1; steps < 6; steps++)
	{
		cout << setw(steps * 5) << "* 0\t" << endl;
		cout << setw(steps * 5) << "*/|\\" << endl;
		cout << setw(steps * 5) << "* /\\" << endl;
		cout << setw(steps * 5 + 2) << "******" << endl;
	}
	cin.get();
	return 0;
}
 * 0	
 */|\
 * /\
 ******
      * 0	
      */|\
      * /\
      ******
           * 0	
           */|\
           * /\
           ******
                * 0	
                */|\
                * /\
                ******
                     * 0	
                     */|\
                     * /\
                     ******
Last edited on
@Condor, YEAH! exactly! wow, how did I not think about using more operational assignments when calculating the with of spaces... Thank you! just a quick question if you can anwser it for me please...

what does the \t do here??
cout << setw(steps * 5) << "* 0\t" << endl;

Thank you for your help
\t is the tab escape sequence it does nothing there since he doesn't put anything after the tab except a new line character :P

You can do something like

 
std::cout << "0\t0" << std::endl;


And depending on your compiler the spaces generated from tab will differ but should look similar to:
0    0


*typo
Last edited on
@AdrianV

You're welcome!

giblit was helpful and answered your question. Many thanks!
@giblit, Thank You I understand better now :D!!

I have done my assignment already, I thank everyone for their help, however I would also like to understand how to do this same assignment but with a nested for loop instead of using setw(), I just wanna learn something new, if anyone can guide me I would be glad. Thank you very much for your help!
replace setw( steps * 5 ) with a for loop :P

1
2
for( int i = 0; i  < steps * 5; ++i )
    std::cout << ' ';


Here's another solution I would suggest over the for loop.

1
2
3
4
5
6
7
8
9
10
11
const std::string indent = "     "; //5 spaces
std::string totalIndent = indent;

for (int steps = 0; steps < 5; ++steps)
{
	std::cout << totalIndent << "* 0\t" << std::endl;
	std::cout << totalIndent << "*/|\\" << std::endl;
	std::cout << totalIndent << "* /\\" << std::endl;
	std::cout << totalIndent << "******" << std::endl;
        totalIndent += indent;
}


*fixed code tag
Last edited on
Topic archived. No new replies allowed.