Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm in a need to write a code which print asterisks in such a way that 1 asterisk for first line, 2 for second line, 3 for third line etc.
Know I'm fully frustrated after trying a lot of times.

Please have an eye. . . . .
C++
#include <iostream>

using namespace std;

int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)
    {
        
        while(colum<row)
        {
            cout << "*" << '\n';
            colum++;
        }
        row++;
    }
    return 0;
}
Posted

I don't normally do people's homework, but you've given it a shot, most people just ask us to do it. You've made two mistakes:

1 - you emit a newline after every *, you need to emit a newline after each group of *s
2 - you don't reset colum to 0, so your rows never get any bigger.

I recommend learning to use your debugger, that should have helped you find both of these issues. I don't know what compiler you're using, but they all suppor breakpoints AFAIK.

C++
#include <iostream>

using namespace std;
 
int main()
{
    int n = 5;
    int row = 0;
    int colum = 0;
    while(row<n)>
    {
        
        while(colum<row)>
        {
            cout << "*";
            colum++;
        }

		cout << "\n";
		colum = 0;
        row++;
    }
    return 0;
}</iostream>
 
Share this answer
 
Comments
Usman Hunjra 1-Jan-13 15:18pm    
Thank you Sir .. I'm using code blocks. Sir please recommend me some more learning stuff about that type of problems. . .
Christian Graus 1-Jan-13 15:20pm    
I have no idea what code blocks is. It's a compiler ? If you want to learn C++, I recommend buying 'The C++ programming language' by Stroustrup, and working through the exercises.
Usman Hunjra 1-Jan-13 15:34pm    
Yes sir its' an open source Compiler .. what about c++ for everyone by Horst Man?
Christian Graus 1-Jan-13 15:43pm    
I don't know it, but I'm sure it's fine, books are expensive, if you have one, work through that one. The one I recommended is from the guy who created the language, if you can get it, I would.
Usman Hunjra 1-Jan-13 15:47pm    
Gotcha ..
Thank u sir for your consideration ...
Inside the outer while loop you need to reset the value of column. Otherwise it only goes through the inner while the first time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900