Pyramid of Asterisk

I know this topic exists but I'm not asking how to do it. I was reading and doing the examples from a book I'm reading. The code for it is pretty straight forward, but not sure if I'm missing something because I'm not getting the output I was expecting. Just wanting input on if I am missing something. You know how your mind fills in what should be there so I need fresh eyes on the code.

OS: Windows Vista Home Basic 32-bit
IDE/Compiler: Code::Blocks/MinG

Book says it should be:
*
* *
* * *
The program output is actually this:
*
**
***

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Example 7-1B
// Program: Prints a triangle of stars
#include <iostream>
using namespace std;

void printStars(int blanks, int starsInLine);

int main()
{
    int noOfLines;   // variable to store the number of lines
    int counter;     // for loop control variable
    int noOfBlanks;  // variable to store the number of blanks

    cout << "Enter the number of star lines (1 to 20) to be printed: ";
    cin >> noOfLines;

    while (noOfLines < 0 || noOfLines > 20)
    {
        cout << "Number of star lines should be between 1 and 20" << endl;
        cout << "Enter the number of star lines (1 to 20) to be printed:";
        cin >> noOfLines;
    }

    cout << endl << endl;
    noOfBlanks = 30;

    for (counter = 1; counter <= noOfLines; counter++)
    {
        printStars(noOfBlanks, counter);
        noOfBlanks--;
    }

    return 0;
}

void printStars(int blanks, int starsInLine)
{
    int count;
    for (count = 1; count <= blanks; count++)
        cout << ' ';
    for (count = 1; count <= starsInLine; count++)
        cout << '*';
    cout << endl;
}
You can't see spaces, so instead of a single space or asterisk, output one followed by a space.

39
40
41
42
43
    for (...)
        cout << "  ";
    for (...)
        cout << "* ";
    cout << endl;

Hope this helps.
Yeah that was the problem. The book did how I had it. Just a typo most likely.
It could just be something the printer did... [edit] The people who printed the book, that is...
Last edited on
Topic archived. No new replies allowed.