By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Loop Problem--help

P: 40
I want to make an output like a Christmas Tree

but...my code shows only half of it...
*
**
***
****
*****
What's wrong with my loop?

for(x=1;x<=5;++x){
for(y=0;y<x;++y){
cout<<"*";
}
cout<<"\n";
}
Mar 7 '07 #1
Share this Question
Share on Google+
6 Replies


Expert 100+
P: 1,510
you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented
Mar 7 '07 #2

P: 40
you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented
just a follow up question...does C++ have a gotoxy(col, row) like in Turbo C? if not what does C++ have in lieu of the col, row in turbo c..thanx for the reply
Mar 7 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
C++ doesn't have this, but if you look up the conio header file, you can use the goto(x, y) function normally.
Mar 7 '07 #4

P: 40
you need to print spaces before the *, 5 on first line, 4 on second, etc so you get the triangle indented
where am i going to put the spaces? before the second for loop or after the two loop? am i going to use "\t"?
Mar 7 '07 #5

Expert 100+
P: 1,510
where am i going to put the spaces? before the second for loop or after the two loop? am i going to use "\t"?
Expand|Select|Wrap|Line Numbers
  1. for(x=1;x<=5;++x){
  2.   <<<   print spaces here!
  3. for(y=0;y<x;++y){
  4. cout<<"*";
  5. }
  6. cout<<"\n";
  7. }
  8.  
to print the spaces you would use a for() loop which decrements (unlike the * which increments)
Mar 7 '07 #6

P: 40
Expand|Select|Wrap|Line Numbers
  1. for(x=1;x<=5;++x){
  2.   <<<   print spaces here!
  3. for(y=0;y<x;++y){
  4. cout<<"*";
  5. }
  6. cout<<"\n";
  7. }
  8.  
to print the spaces you would use a for() loop which decrements (unlike the * which increments)
ok..thanx a lot for the replies.. =)
Mar 7 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.