[cs13001] Professor, lab help?

Mikhail Nesterenko mikhail at cs.kent.edu
Sat Sep 15 19:27:15 EDT 2012


> I have come so close to solving this program, but I have a problem with the
> last figure. The first two figures are fine, but the last is nothing but a
> line of *s... I have tried it every which way, but I can't quite grasp it.
> This is my last figure:
> for (int w = 1; w <= x ; w++) {
> for (int m = 1; m <= w; m++)
> cout << "*"; }
> For some reason that I can't find, it remains a line of stars
> "********************************************"
>  even if I make it a copy of the first two figures, regardless of the input
> number.

First, you should not be guessing what your program is doing. You
should be tracing through it, executing it line by line, to find out
what exactly is going on.

Presently, your code is printing a single line of stars because it is
not inserting endline characters:

	cout << endl;

at correct intervals. Think where you'll need to put the above
statement. Once you fixed this, your code will print a
rectangle. However, you'll need a triangle. To get the triangle, in
the inner loop, instead of just printing a star, you'll need to
decide what to print: a star or an empty space:

       cout << ' ';

This decision should be carried out by a branching statement and it
should depend on the values of your loop variables.

BTW, to make your code easier to read and understand, you should
indent it properly.

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list