Array output formatting

Hello all,

I have stumbled upon another issue with some of my classwork. For this part, we have to display the information from a dynamic array in a neat and organized list, where the "empty" spots say empty and the spots that contain someone shows their ID number(passed by the array). My main issue is trying to differentiate between empty and "full", I think at some point in the program I should fill the arrays with -1 but I have not been able to figure that out. Also, I am having a little issue with formatting but id rather have it work in a bad format than not work at all. Below I have attached what code and functions I have, and what the menu should look like. Sorry for any terrible coding or amateur mistakes. I have been working on this for about 6~ hours straight and I'm ready to hear some ideas from someone else. Also if you see something that could be better or cleaned up, don't hesitate to share! I am still quite new to c++ and always looking to get better

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
45
46
47
48
49
50
51
52
void display(int **labPtr);//displays an entire lab



int main()
{
    initialDisplay(); //used to display the universities
    int userChoice = 0; //used to control menu


    //below is code initializing the parallel arrays and pointers
    int* labPtr[NUMLABS]={nullptr};
    for(auto i=0; i<NUMLABS; ++i){
        labPtr[i]=new int[i];
    }

    string* namePtr[NUMLABS]={nullptr};
    for(auto i=0; i<NUMLABS; ++i){
        namePtr[i]=new string[i];
    }

    int* timePtr[NUMLABS]={nullptr};
    for(auto i=0; i<NUMLABS; ++i){
        timePtr[i]=new int[i];
    }


        do{
            mainMenu(); //displays menu

            cout << "Your Choice: ";
            cin >> userChoice; //recives choice for the menu

            switch (userChoice)
            {
                case(1):
                    logIn(labPtr, namePtr, timePtr); //runs login function if chosen
                    break;
                case(2):
                    logOff(labPtr, namePtr, timePtr); //runs log off function if chosen
                    break;
                case(3):
                    look(labPtr); //runs the search function if chosen
                    break;
                case(4):
                    display(labPtr); //displays a chosen lab if chosen
                    break;
                case(5): //closes menu and ends program
                    break;
            }
        }while(userChoice != 5);
}


Here is the function that i have bee able to come up with so far but its not even close
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
void display(int **labPtr){

    int labNumber;

    cout << "Enter the lab number user wishes to view: ";
    cin >> labNumber;


    if(labNumber<1 || labNumber>NUMLABS)
    {
       cout << "Invalid lab number."
           << " Try to enter the lab number between 1 and 8\n";
       return;
    }

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

   for(int j = 0; j < LABSIZES[labNumber-1]; j++){

        cout << (j+1) << " : ";

        if(labPtr[labNumber-1][j] == -1)
           cout << "empty" << setw(3) << " ";



   cout << endl;
   cout << "--------------------------------------------------------------------------------" << endl;
}


and the menu example that was given looks like

LAB STATUS
Lab # 2 for "university names"
Computer Stations
1 : empty 2 : 27739 3 : empty 4 : empty 5 : empty
6 : empty 7 : empty 8 : empty 9 : 18693 10: empty
11: empty etc... etc...

(labs are as small as 10 and as large as 30 and the display should change respectively)
One way to handle these arrays of pointers, is to only use new where there's something to store, and nullptr if it's empty.

Then, checking for an empty slot is easy, it's just checking for nullptr.

Topic archived. No new replies allowed.