increase/decrease nesting loop trouble

February 21, 2011 at 11:41:03
Specs: Windows Vista
Trying to get an outcome that looks like this..
#
?#
??#
?#
#

What I'm getting is this...
#
?#
?#?#

compiles ok, but as you can see it places the # after each ? rather than grouping them together, and it neglects to count back down.
Don't want to have my homework done for me, just want an idea where to look for the trouble. head spinning.

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{
char bend = '#';//#@ beginning and end
char cues = '?';
int n,x,y ;
cout <<"Please enter number of ?'s :";
cin >> n;
cout<< bend<< endl;
for(y=1;y<=n;y++)
{
for(x=1;x<=y;x++)
{
cout<< cues <<bend;
}
cout<< endl;
}
for(y=1;y>=n;y--)
{
for(x=1;x>=y;x--)
{
cout<< cues << bend;
}
cout<< bend << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}


See More: increase/decrease nesting loop trouble

Report •

#1
February 22, 2011 at 11:01:26
looking at the desired effect alone, it would seem that you'd want to print the '#' after the loop printing the '?' terminates instead of printing both in the same loop.

for going back down, start with y = n and run through y >= 0 (to catch the last '#') - decrement the value of y. x can start at 1 with the condition being <= y and increment its value...

Not checked, but I *think* that will do.



Report •

#2
February 22, 2011 at 17:35:59
Said another way:
cout<< cues <<bend; is equivalent to cout << '?' << '#';. If you don't want to print a # for every ?, then you shouldn't pair them up. Instead, try pairing one of them up with this line: cout<< endl;

How To Ask Questions The Smart Way


Report •

#3
February 23, 2011 at 10:05:49
thanks guys, I ended up having to put "x=y-1" in my last "for" statement, but it looks pretty darn good. Thanks for the help.
Now it looks like this:
#
?#
??#
???#
??#
?#
#
#
Ill work on it in class and hopefully get rid of that last #

Report •
Related Solutions


Ask Question