Sign in to follow this  
OneThreeThreeSeven

for loop help O-o

Recommended Posts

#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 ***** ****. ***.. **... *....

Share this post


Link to post
Share on other sites
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 << ".";
}


:)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this