A little table

I'm making a table with 10 columns and 5 rows. I know one way to do this:

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
#include <iostream>

using namespace std;

int main()
{
    cout << "  ";
    for (int numrow=1; numrow<=10; numrow++) {
        cout << "    " << numrow;
        if (numrow == 10) {
            cout << "\nA";
        }
        else {
            continue;
            }
            for (int Arow=1; Arow<=11; Arow++) {
                cout << "  |  ";
                if (Arow == 11) {
                    cout << "\n\nB";
                }
                else {
                    continue;
                    }
                    for (int Brow=1; Brow<=11; Brow++) {
                        cout << "  |  ";
                    }
            }
    }
    return 0;
}


But this way is quite painful and l o n g, and the amount of rows/columns isn't easily editable, and as you can see, I am only on two rows. I am also getting hung up on a lot of missing brackets and stuff when I'm ABSOLUTELY sure I don't need to be.

I'm pretty sure the better solution requires only two nested for loops, but don't give me the answer please...just hints...unless I'm doomed (in that case I will let you all know).
closed account (o3hC5Di1)
Think about a table in this way:

A table consists of rows, rows consist of cells. (These cells form columns).
You can only output data sequentially (ie, you can not output a column, and then put another one next to it, only beneath it).

So you write a table line per line (row per row).

So you will have to tune a row-loop 5 times, and within that loop, you will do another loop that prints the amount of cells you want in each row.

Hope that helps.

All the best,
NwN
That's enlightening, I'll work on it based on what you told me...
Last edited on
Okay, I got it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include <iostream>
using namespace std;

int main()
{
    cout << " ";
    for (int TopRow = 1; TopRow <=10; TopRow++) {
        cout << "    " << TopRow;
    }
    cout << "\n";
    char LetRow = 'A';
    for (int NextRow = 1; NextRow <=10; NextRow++) {
        cout << LetRow;
        LetRow++;
        for (int SubRow = 1; SubRow <=11; SubRow++) {
            cout << " |   ";
        }
        cout << "\n\n";
    }
}


~Solved~
Last edited on
Topic archived. No new replies allowed.