Hi I am trying to write a small C++ program using nested loops so that when the program executes I have the following output displayed

**
****
******
********
**********
************

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.

Thank you Steve Swickard

//I include any header files I need here
#include <iostream>
#include <string>
using std::cin;
using std::string;
using std::cout;
using std::endl;
 
int main()
{
//I declare variables and constants here
const char STAR ='*';
int i =0;
int j =0;
int k =0;
 

for(; i<7; i++) // my first loop or out loop to execute
//6 times
 
{

for (;j<k;j=j++) // this loop determines how many * to print
{
cout << STAR;
}
{ 
for (;k !=14; k=k+2) // this loop determines the max # of *
//to print
 
cout << endl;
}
}
return 0;
 
} //end of main function

Format your code properly with code tags and finish your for statements (3 parameters, not 2) and repost...

Then we have something readable to work with.

Format your code properly with code tags and finish your for statements (3 parameters, not 2) and repost...

Then we have something readable to work with.

#include <iostream>
#include <string>
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main()
{
const char STAR ='*';
int k=2;
for(int i=0; i<7; i++)
{

for(int j=0;j<k;j=j++)
{
cout << STAR;
}
{ 
for(int k=0;k < 14; k= k+2)
cout << endl;
}
}
return 0;
} //end of main function

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

If I understand right I needed to enclose my code in [] right?

No. You enclose the code in tags. You could possiblly use the PREVIEW button to see if your post looks 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.

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.

Formatted properly, you code looks like:

#include <iostream>
#include <string>
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main()
{
    int i;
    int j;
    int k;

    const char STAR ='*';
    k=2;                    // What does this statement do for you?
                            // 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;
        }
    }
    return 0;
} //end of main function

I don't see the reason for the 3rd loop

for(int k = 0; k < 14; k += 2)
  cout << endl;

You'll be outputting 7 newlines between rows of stars... is that what you meant to do?

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.

Formatted properly, you code looks like:

#include <iostream>
#include <string>
using std::cin;
using std::string;
using std::cout;
using std::endl;
int main()
{
    int i;
    int j;
    int k;
 
    const char STAR ='*';
    k=2;                    // What does this statement do for you?
                            // 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;
        }
    }
    return 0;
} //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

Steve

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.

Hmmmm.... no comment :)

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, ...

Good thought.

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?

Hmmmm.... no comment :)


Good thought.


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?

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

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

Thank you. I am currently coding the program and I will see if I have it thank for your help

Steve

WaltP,

I just wanted to say thank you once again. I have successfully compiled and run the program and I now have the desired output. Rather simple actually although I was trying to make it way to complicated. I am glad you forced me to take a good look at the problem and see relationships. Have a great weekend

I sincerely apologize to all forum members for not properly formatting and using tags in my code I will learn how to use tags for any future request for help. Oh and than you David for properly formatting my code and adding the necessary tags.

Steve

i want to solve this
1
2 3
4 5 6
7 8 9 10

commented: Don't bump old threads -1

reply pls

Wow I had forgotten about my little thread from back when I was in college.

vivek14, maybe you should start a new thread with a little more info.

Thanks

Steve

code in c++ to print :
*
***
*****
***
*

So, steveincolorado, whats the correct coding for your program??
pls comment. .

commented: necro bump +0
1. #include<iostream>
2. #include<conio.h>
3. using namespace std;
4. int main()
5. {
6.     int a=1;
7.     for(int i=1;i<=4;i++)
8.     {
9.             for(int j=1;j<=i;j++)
10.             {
11.                     cout<<a;
12.                     a++;
13.             }
14.             cout<<endl;
15.     }
16.     getch();
17. }
Output
  1. 1
  2. 2 3
  3. 4 5 6
  4. 7 8 9 10
    1.     #include<iostream>  
    2.     #include<conio.h>
    3.     using namespace std;
    4.     int main()
    5.     {
    6.         for(int i=1;i<=5;i+=2)
    7.         {
    8.                 for(int a=1;a<=i;a++)
    9.                 {
    10.                         cout<<"*";
    11.                 }
    12.                 cout<<endl;
    13.         }
    14.         for(int j=3;j>=1;j-=2)
    15.         {
    16.                 for(int b=j;b>=1;b--)
    17.                 {
    18.                         cout<<"*";
    19.                 }
    20.                 cout<<endl;
    21.         }
    22.         getch();
    23.     }
Output
  1. *
  2. ***
  3. *****
  4. ***
  5. *
1.     #include<iostream>
2.     #include<conio.h>
3.     using namespace std;
4.     int main()
5.     {
6.         for(int i=2;i<=12;i+=2)
7.         {
8.                 for(int a=1;a<=i;a++)
9.                 {
10.                         cout<<"*";
11.                 }
12.                 cout<<endl;
13.         }
14.         getch();
15.     }
Output
  1. **
  2. ****
  3. ******
  4. ********
  5. **********
  6. ************
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.