Jump to content
  • Advertisement
Sign in to follow this  
OneThreeThreeSeven

for loop help O-o

This topic is 3103 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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 << ".";
}


:)

Share this post


Link to post
Share on other sites
for(int a = 0; a < number2; ++a)
{
cout << "*";
}

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

and do not decrement number3

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!