Here is the code I have written. I seem to be having a problem with proper loop execution. I would appreciate any help in determining where I have gone wrong. I print out 14 stars ad that is it.
I believe I have it right. I apologize as this is my first post with code. If I understand right I needed to enclose my code in [] right? I re-wrote the for statements although I thought that it was okay to declare variables earlier in the program and just place a ";" in the place of the the variable declaration in the for statement? I was unable to get the k variable to compile without first declaring it.
Thank you for any assistance you might provide
Steve
Last edited by Dave Sinkula; Sep 9th, 2006 at 7:35 pm. Reason: Fixed [code][/code] tags.
I re-wrote the for statements although I thought that it was okay to declare variables earlier in the program and just place a ";" in the place of the the variable declaration in the for statement? I was unable to get the k variable to compile without first declaring it.
Yes you should declare your variables earlier in the program. And yes you can just ";" if you know what you're doing. The first position is not a variable declaraction is a variable initialization, the first value the variable has thru the loop.
Also, formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent.
No. You enclose the code in tags. You could possiblly use the PREVIEW button to see if your post looks right...
Yes you should declare your variables earlier in the program. And yes you can just ";" if you know what you're doing. The first position is not a variable declaraction is a variable initialization, the first value the variable has thru the loop.
Also, formatting is of primary importance. Every time you use { indent 4 spaces. Just before you use every }, unindent.
// What's the value the second time thru the I loop?
for(i=0; i<7; i++)
{
for(j=0;j<k;j=j++)
{
cout << STAR;
}
{// Why are you starting a new block?
for(k=0;k < 14; k= k+2)
cout << endl;
}
}
return0;
}//end of main function
WaltP it never occured to me at the time to preview my post but your absolutely correct and thank you for the pointer on initialization of the variable.
My thinking was that I since I needed to print six lines of star's that I would have the outer loop run six times, then I needed an inner loop to run six times but increment by 2 such that I would have another loop run 2 times, then 4, then 6, then 8, 10 and finally 12 times so that my cout <<STAR; statement would be printed by the same number each time through so that I would have the following output:
** print 2 times
**** print 4 times
****** print 6 times
******** print 8 times
********** print 10 times
************ print 12 times
I seem to have one loop running while the others do not run or they seem not not run I just want to know where I am going wrong thank you
WaltP it never occured to me at the time to preview my post but your absolutely correct and thank you for the pointer on initialization of the variable.
then I needed an inner loop to run six times but increment by 2 such that I would have another loop run 2 times, then 4, then 6, then 8, 10 and finally 12 times so that my cout <<STAR; statement would be printed by the same number each time through so that I would have the following output:
** print 2 times
**** print 4 times
****** print 6 times
******** print 8 times
********** print 10 times
************ print 12 times
You just complexed it out of reason.
Let's think about this... (yeah, I know that's what we're doing... )
You have one outer loop that goes from 0 to 6 for(i=0; i<7; i++)
That's 7 lines... What's wrong here? That you can fix easily.
Now you want 6 lines of 2, 4, 6, 8... *s. Your loop goes 0, 1, 2, 3...
Is there a correlation between what you want and your loop?
Let's think about this... (yeah, I know that's what we're doing... )
You have one outer loop that goes from 0 to 6 for(i=0; i<7; i++)
That's 7 lines... What's wrong here? That you can fix easily.
Now you want 6 lines of 2, 4, 6, 8... *s. Your loop goes 0, 1, 2, 3...
Is there a correlation between what you want and your loop?
Okay for your first question I should change the for statement to i<6
Yes now that you have pointed it out I see that there is a correlation between the two sets.
My outer loop is:
0, 1,2,3,4,5
the difference between that and what I need to print is
2,3,4,5,6,7
so the inner loop actually would appread to be the outer loop number +1 right?
so I could add 1 to i for the inner loop such as j=i+2; ?
Okay for your first question I should change the for statement to i<6
Yes now that you have pointed it out I see that there is a correlation between the two sets.
My outer loop is:
0, 1,2,3,4,5
the difference between that and what I need to print is
2,3,4,5,6,7
so the inner loop actually would appread to be the outer loop number +1 right?
so I could add 1 to i for the inner loop such as j=i+2; ?
Thank you
In both cases -- BINGO! Two loops is all you need. One outer for the lines, one inner for the *s based on teh line you're on...
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.