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?