Click here to Skip to main content
Sign Up to vote bad
good
See more: C++Homework
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. . . . .
#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 1 Jan '13 - 9:09


2 solutions

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.
 
#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>
  Permalink  
Comments
Scenic Design - 1 Jan '13 - 15:18
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:20
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.
Scenic Design - 1 Jan '13 - 15:34
Yes sir its' an open source Compiler .. what about c++ for everyone by Horst Man?
Christian Graus - 1 Jan '13 - 15:43
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.
Scenic Design - 1 Jan '13 - 15:47
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.
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 253
1 Rohan Leuva 220
2 Sergey Alexandrovich Kryukov 172
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,528
1 OriginalGriff 6,819
2 CPallini 3,603
3 Rohan Leuva 2,923
4 Maciej Los 2,268


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid