Stairs

Pages: 12
I have this program to do with the following program and I am not sure how to go about doing it.
Write a program that prompts for number of stairs and then displays the requested number of stairs with a person standing on each stair. Use the letter 'o' for the head, the forward and backward slashes for the hands, the bar for the body and the backward and forward slashes for the legs. Use asterisks for the floor and the wall (see diagram below). The program should use loops to diplay multiple lines and multiple spaces. You need to come up with the right formula for varying where a stair is displayed and varying the number of spaces both before a stair and after a stair. Note that some the number of spaces before a stair is decreasing as you go from top stair to bottom stair while number of spaces after the stair is increasing.

Here is an example where a user entered 5 for the number of stairs.

*******
o * *
/|\ * *
/ \ * *
******* *
o * *
/|\ * *
/ \ * *
******* *
o * *
/|\ * *
/ \ * *
******* *
o * *
/|\ * *
/ \ * *
******* *
o * *
/|\ * *
/ \ * *
*************************************

Use 'CASE'. and assign each 'number of a stairs' to be taken a unique cout.. for example if case is 1 that is only one stair just cout the star diagram showing only 1 stair. as the case number increases just increase the number of *stairs being outputted. try to write the program, if you still can't do it just let me knw. (",).
You are going to be repeating the drawing of a stair/figure/wall a fixed number of times based on user input. This is an ideal situation to use a for loop.

Begin by creating the program to display as posted above. After that works, then add spacing to get the stairs to step up to the right.
I am still having difficulties getting it to run correctly, I not real familiar with using case, we were just asked to use loops, I am just a beginning computer programing student and I am really have difficulty getting an understanding of the outline of the loops.
We do have to use a for or while loop. I have to have this complete asap. I am beginning to fall behind in my work because of this one project.
This is what I have so far but it doesn't look right. Help please!

#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
cout << "Enter number of stairs: " << endl;
cin >> num >> endl;

for (num = 1)
{ cout << " o *" << endl;
cout << " /|\ *" << endl;
cout << " / \ *" << endl;
cout << "*******" << endl;
}
Okay. Okay...

I seriously shouldn't give you the completed code, because that would be no fun. However, here's a few tips I can give...

1: Exclude <string>, as you don't need it.

1.1: Optionally, use <stdio.h>. I personally like it a LOT more.

1.2: cin >> num >> endl; would look like scanf("%d",num); with stdio.h.

2: TYPE CHECKING. If someone enters "five" instead of 5, your program is screwed. Although considering who you're showing this program to, it might not be mandatory.

3: The for loop should look like this...
1
2
3
4
for (int i = 0; i < num; i++)
{
   //insert your printfs or couts here.
}


3.1: The while loop would look like this...
1
2
3
4
5
6
int i = 0;
while (i < num)
{
   //insert your printfs or couts here.
   i++;
}


4: Don't forget to return 0.

4.1: If you don't want to return anything, use void main() you have to return something. Even if it's 0.

Hope that helped!

P.S.- Varying the position of the stairs calls for another for loop within the for loop.
Last edited on
I t supposed to have four loops do I just repeat the for loop for each step?
This is what I got but it still isn't letting me compile it.

//project4.cpp
//Quinton Fryman
//April 15,2010
//This Program is designed to build stairs from a prompt of how many steps using loops.

#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
cout << "Enter number of stairs: " << endl;
cin >> num >> endl;

int i = 0;
while (i < num);
{
cout << " o * *" << endl;
cout << " /|\ * *" << endl;
cout << " / \ * *" << endl;
cout << "*************************************" << endl;
i++;
}
int i = 1;
while (i < num);
{
cout << " o * *" << endl;
cout << " /|\ * *" << endl;
cout << " / \ * *" << endl;
cout << " *******************************" << endl;
i++;
}
int = 2;
while ( i < num);
{
cout << " o * *" << endl;
cout << " /|\ * *" << endl;
cout << " / \ * *" << endl;
cout << " ************************" << endl;
i++;
}
int = 3;
while ( i < num);
{
cout << " o ************" << endl;
cout << " /|\ * *" << endl;
cout << " / \ * *" << endl;
cout << " ******************" << endl;
i++;
}
return 0;
}
Okay.

Seeing as you appear to be really new to C++, I suppose I'll just slip up and accidentally give you the code for the for loop chain. I can't guarantee that it will work perfectly, as I came up with it in literally 5 minutes, however...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int q, v, b;
for (int i = 0; i < num; i++)
{
   for (q = 0; q < 4; q++)
   {
       for (v = (i+1); v < num; v++)
          cout << "         ";
       switch (q)
       {
         case 0:
         cout << " o * *   ";
         break; case 1;
         cout << "/|\ * *  ";
         break; case 2;
         cout << "/ \ * *  ";
         break; case 3;
         cout << "******* *";
       }
       for (b = 0; b < i; q++)
          cout << "         ";
      cout << "\n";
   }
}


-Albatross
Last edited on
I don't understand very much of that for statement but I put it into the program and it didn't compile. I am really ignorant when it comes to this stuff so I wouldn't know where to start fixing this problem. Sorry for any inconvenience I may have caused.
Hey, don't worry about it. We all had to start somewhere, and it was no inconvenience.

I was hoping that you'd notice the errors that I inserted to force you to think a bit as to why it's not working, but... anyways.

Let's try extending it a bit...

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
//project4.cpp
//Quinton Fryman
//April 15,2010
//This Program is designed to build stairs from a prompt of how many steps using loops.
//Written with help from an albatross.
#include <stdio.h>

int main()
{
int num=5;
printf("Enter the number of stairs you wish to print: ");
scanf("%d", &num);
printf("\n\n");
int q, v, b;
for (int i = 0; i < num; i++)
{
   for (q = 0; q < 4; q++)
   {
       for (v = (i+1); v < num; v++)
          printf("          ");
       switch (q)
       {
         case 0:
         printf(" o * *   *");
         break; case 1:
         printf("/|\\ * *  *");
         break; case 2:
         printf("/ \\ * *  *");
         break; case 3:
         printf("******* *");
         break; default:
         printf("We got a problem here, Sarge!");
         break;
       }
       for (b = 0; b < i; q++)
          printf( "          ");
      printf("\n");
   }
}
return 0;
}
Last edited on
OK I understand everything but the printf statements, the compiler doesn't recognize it. I says unknown escape sequence, I have no clue what this means.
The picture in the problem statement is supposed to be a man standing on each of 5 steps on a stair case although it doesn't look that way. However if you go to the following link you'll see the problem statement.

http://district.bluegrass.kctcs.edu/robert.chirwa/cs115/projects/proj3.htm
Oh... right. I can't just put in a backslash, it has to be two next to each other.. I need to work on my printf...

I have edited the post accordingly.

Also, it somehow doesn't like multiple for loops. Odd... however.

-Pidgeon
Last edited on
Thanks for the help but that didn't give me the output i need, I need the figure to be on each step of five inclining to the right.

http://district.bluegrass.kctcs.edu/robert.chirwa/cs115/projects/proj3.htm
Last edited on
That's just it... the first step it prints (for me) does incline the right amount to the right, but the next one gets only the first row printed, and judging by the performance strain on my laptop (which is the top of its line), something's entered an infinite loop. I'll try a while loop, it shouldn't make any difference and yet it did for me several times.

The code below works perfectly, but it doesn't display the row at that bottom or eliminate the side "walls". To do that, add another for loop just before return 0; to print (the number of stairs you ordered * 9 or 10) asteriks. To deal with the other one, add an if-else clause encompasing the switch. Put the switch in the else statement, copy it to go into the if statement, and then mess with the printfs of the one in the if statement (the conditions of the if statement should be (i < 1)

Final working code (mess around with the printfs to get it looking right):
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
53
54
55
56
57
58
//project4.cpp
//Quinton Fryman
//April 15,2010
//This Program is designed to build stairs from a prompt of how many steps using loops.
//Written with help from an albatross.
#include <stdio.h>

int main()
{
	int num=5;
	printf("Enter the number of stairs you wish to print: ");
	scanf("%d", &num);
	printf("\n\n");
	int q, v, b, i;
	i = 0;
	while (i < num)
	{
        //Start printing stairs.
		q = 0;
		for (q = 0; q < 4; q++)
		{
                        //Print each line of each stair
			v = (i+1);
			b = 0;
			while (v < num)
			{
                                //Add spaces before each stair.
				printf("         ");
				v++;
			}
			switch (q)
			{
                                //Print the actual body on the stair, depending on the iteration of the loop this is in.
				case 0:
					printf(" o       *");
					break; case 1:
					printf("/|\\      *");
					break; case 2:
					printf("/ \\      *");
					break; case 3:
					printf("**********");
					break; default:
					printf("We got a problem here, Sarge!");
					break;
			}
			while (b < i)
			{
                                //Add spaces after each stair.
				printf("         ");
				b++;
			}
                        //Print an asterisk and a move to the next line.
			printf("*\n");
		}
		i++;
	}
return 0;
}



-Albatross
Last edited on
This works ok but I gotta tell I don't understand any of it. I there anyway you can copy some comments onto the program so I can see what does what. I really appreciate your help.
Albatross wrote:
1: Exclude <string>, as you don't need it.


You might need it. In many implementations, string is included in iostream.

Albatross wrote:
1.1: Optionally, use <stdio.h>. I personally like it a LOT more.

2: TYPE CHECKING. If someone enters "five" instead of 5, your program is screwed. This is why I recommend stdio.h, it does a form of type checking for you.

2.1: cin >> num >> endl; would look like scanf("%d",num); with stdio.h.


I strongly recommend ignoring this advice and sticking to C++. stdio.h should be deprecated, as far as I'm concerned.

For loops are explained in the tutorial, here:
http://cplusplus.com/doc/tutorial/control/#for

Albatross wrote:
4: Don't forget to return 0.


That's good advice. main always returns an int--according to the standard.

Albatross wrote:
4.1: If you don't want to return anything, use void main()


That would be non-standard. Please do not do that. Return an int.
Listen, it's not very often that I speak up and challenger another members post but this gem is definitely not an example that you want to follow. The following snippet is a loop, where i is 0 - 3. Then there is a switch statement that covers the 4 cases. How about just using 4 cout statements and calling it a day?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
   for (q = 0; q < 4; q++)
   {
//...
       switch (q)
       {
         case 0:
         cout << " o * *   ";
         break; case 1;
         cout << "/|\ * *  ";
         break; case 2;
         cout << "/ \ * *  ";
         break; case 3;
         cout << "******* *";
       }
//...
   }


Could just be:
1
2
3
4
         cout << " o * *   ";
         cout << "/|\ * *  ";
         cout << "/ \ * *  ";
         cout << "******* *";
Pages: 12