Formatting a 2d array output

Hey yall, so this is one of the last things I should do for my project and I can't seem to come up with a good idea for it so I thought I would bring it here. I have to display a "lab" of "computers" and show which ones are empty. Below is the code I have so far, and it outputs it in a big list. I just wanted to see if there was some way to get it to print with 5 stations to a line.

So instead of
#1: empty
#2: empty
#3: empty
#4: empty
#5: empty
#6: empty
#7: 13432

it is
#1: empty #2: empty #3: empty #4: empty #5: empty
#6: empty #7: 13432 ........

The main issue I'm having is trying to wrap my head around the 2d arrays and such, so any help would be greatly appreciated. Thanks!

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
  void display(int **labPtr){ //displays the given lab

    int labNumber;
    bool loopFlag = true;

   do{
		cout << "Enter the lab number you wish to view (1-8): ";
		cin >> labNumber;
		loopFlag = validateInput(labNumber, 1, 8);
	} while (loopFlag);

   cout << "LAB STATUS" << endl;
   cout << "Lab # " << labNumber << " for " << UNIVERSITYNAMES[labNumber-1] << endl;


            for(auto j=0; j<LABSIZES[labNumber-1]; ++j)
            {
                 if(labPtr[labNumber-1][j] == -1 || labPtr[labNumber-1][j] > 99999 || labPtr[labNumber-1][j] < -99999)
                 {
                     cout << "#" << j+1 << ": empty" << "\n";
                 }
                    else{
                        cout << "#" << j+1 << ": "<<std::setfill('0') << std::setw(5) << (labPtr)[labNumber-1][j] << "\n" << std::setfill(' ');
                        }
            }

   cout << endl;
   cout << "--------------------------------------------------------------------------------" << endl;
}
Not sure which problem you want help with.

I just wanted to see if there was some way to get it to print with 5 stations to a line.

You could use a separate counter, but j is already counting for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
   for (auto j = 0; j < LABSIZES[labNumber - 1]; ++j)
    {
        if (labPtr[labNumber - 1][j] == -1 || labPtr[labNumber - 1][j] > 99999 || labPtr[labNumber - 1][j] < -99999)
        {
            cout << "#" << j + 1 << ": empty" << "\n";
        }
        else 
        {
            cout << "#" << j + 1 << ": " << std::setfill('0') << std::setw(5) << (labPtr)[labNumber - 1][j] << "\n" << std::setfill(' ');            
        }        
        if (j % 5 == 4)     //  Have we printed 5?
            cout << endl;
    }


The main issue I'm having is trying to wrap my head around the 2d arrays and such, so any help would be greatly appreciated. Thanks!

What exactly are you having problems with?
Printing some data with n items per line doesn't really have much to do with 2d arrays. If you count how many items have been printed, then using the mod operator (%) will indicate whether you need to output a newline or not. As per AbstractionAnon's code above L11-12.
Thank you AbstractionAnon, your idea worked, I also had to remove the "\n" from my lines in the main loop but it works exactly as expected. What I meant by having problems meant I am still new to this stuff and this array project I have been working on has been very difficult. I knew it didn't have much to do with them, but I am very surprised the answer was just some modulus and that was it. Thank yall again.
Topic archived. No new replies allowed.