Formatting output a certain way

My question is, how do I get my output to display like the photo below?

If you run my code, you'll see that everything works, but the numbers don't have the correct amount of space between them. Also, when I input a single digit number, like one for example, the first two rows don't line up with the others. I've tried using setw, but still couldn't get it to work.

Here's a link to how it should look: http://i66.tinypic.com/2wlugls.jpg
Here's what mine looks like: http://i64.tinypic.com/2rwb9k8.jpg

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
// Program prompts user to enter integer from 1-100
// and then using a loop construct, print values
// starting from the value entered to 100.
// In addition, print 8 numbers per line.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int minNumber;
    int maxNumber = 100;

    cout << "Enter a number: ";
    cin >> minNumber;

    while ( minNumber < 1 || minNumber > 100 )
    {
        cout << "Invalid number. Enter a number between";
        cout << " 1 and 100.\n";
        cout << endl;
        cout << "Enter number: ";
        cin >> minNumber;
    }

    int newLine = 0;
    for ( int number = minNumber; number <= maxNumber; number++)
    {
        cout << number << " " " " " ";

        if ( ++newLine == 8 )
        {
            cout << endl;
            newLine = 0;
        }
    }
    return 0;
}
Here's an update. I added:

1
2
3
4
5
6
7
8
9
10
11
int newLine = 0;
    for ( int number = minNumber; number <= maxNumber; number++)
    {
        cout << setw(3) << fixed << number << " ";

        if ( ++newLine == 8 )
        {
            cout << endl;
            newLine = 0;
        }
    }


and now my output looks like this: http://i64.tinypic.com/2dh8ope.jpg

The numbers are still stacked too close together.
Why didn't you try 72?
The expected output starts with 72.
It's just an example of what it should look like. The program should take any input from the user between 1-100 and display the numbers from what they input to 100.
Last edited on
closed account (48T7M4Gy)
Increase the value of setw(3) eg setw(10)

and the line need only be cout << setw(10) << number;


Enter a number: 20
        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        45        46        47        48        49        50        51
        52        53        54        55        56        57        58        59
        60        61        62        63        64        65        66        67
        68        69        70        71        72        73        74        75
        76        77        78        79        80        81        82        83
        84        85        86        87        88        89        90        91
        92        93        94        95        96        97        98        99
       100 
Exit code: 0 (normal program termination)
Last edited on
This is what changing to 10 did: http://i65.tinypic.com/16kqnaq.jpg
Oh, I see what I did wrong. I forgot to delete the space. Thank you @kemort
Last edited on
closed account (48T7M4Gy)
Hint: Sometimes to see what goes on put a '*' or "*" temporarily instead of a space.

For the future: Also some formatting is 'sticky'.

http://stackoverflow.com/questions/1532640/which-iomanip-manipulators-are-sticky
Last edited on
Ok, thanks for the tip. I had another question though. Why is there a larger space between the columns? In the example of how it should look, the columns are closer together.
closed account (48T7M4Gy)
Basically setw(xx) sets the column width to xx. Try various values.

Checkout the tutorials and reference material here for more comprehensive details on how <iomanip> functionality works.
Thank you. Gah, I can't wait until I can stop asking these newbie questions and finally understand what the hell it is I'm doing. We keep getting jam packed with new info every few days, I barely have time to go over what we just learned.
closed account (48T7M4Gy)
That's OK. It's all part of C++.

Don't forget to try things out with small test programs of a few lines. Above all, you're the boss, not the machine, there is no magic, and nothing will break. :)
Oh, I've been trying. This program has given me the most trouble so far. I've been working on it non-stop for the past few days and something else just caught my eye.

The 100 for my output isn't lined up correctly. I thought setw fixes that?
closed account (48T7M4Gy)
What you have to do is play around with the number of columns per line. ie vary the number 8 in newline.
I would mess around with that except it's in our instructions to print 8 per line for this program.
closed account (48T7M4Gy)
If you are required to have 8 columns per line then what you see is what you get unless the number chosen to start with gives you an exact number of columns/lines

eg enter enter 21
Topic archived. No new replies allowed.