for loop help O-o

Started by
5 comments, last by rip-off 14 years, 3 months ago

#include <iostream>

using namespace std;

int main()
{
	cout << "Enter a number: ";
	int number;
	cin >> number;
	int i = 0;
	int number2 = number;
	int number3 = number;

	while(i < number)
	{
		for(int a = 0; a < number2; ++a)
		{
			cout << "*";
		}	
		
		for(int b = number; b < number3; --b)
		{		
			cout << ".";
		}
	
		
		--number2;
		--number3;
		++i;
		cout <<"\n";
	}

	return 0;

}

Why does it ignore the 2nd loop? For example, if I put 5, the output is: ***** **** *** ** * I don't understand why it ignores the 2nd for loop. It should look like ***** ****. ***.. **... *....
Advertisement
this part of your code has problem
for(int b = number; b < number3; --b)		{					cout << ".";		}

you are decrementing so it should be like

for(int b = number; b > number3; --b)
{
cout << ".";
}


:)
homework question?
taytay
for(int a = 0; a < number2; ++a)
{
cout << "*";
}

for(int b = number2; b < number3; ++b)
{
cout << ".";
}

and do not decrement number3
also your algorithm is wrong.
taytay
Quote:Original post by ahmedkl
this part of your code has problem
for(int b = number; b < number3; --b)		{					cout << ".";		}

you are decrementing so it should be like

for(int b = number; b > number3; --b)
{
cout << ".";
}


:)


ahh that was it. Just had the wrong symbol in there. ;)

Thx all.
Hint: you can do this without the "number2" and "number3" variables. The value of "number" doesn't change during the algorithm, so you should be able to make all your loops work from the value of "number" and "i".

Also, why did you make your outer loop a "while" loop, when it has the same (init; condition; step) sequence as your inner loops?

This topic is closed to new replies.

Advertisement