1

I've been working on this assignment for class for about a month now. It's way past due and I'm trying to finish it more for my own benefit so I understand how dynamic arrays and pointers work. I'm still quite new to C++ and would appreciate any information you can provide. I thank you for your time ahead of time.

Take note id, cstation, lab are other variables used in other parts of the program to create a menu, search and login functions and are working properly.

The error I'm getting with my code is:

project_9_5.cpp: In function ‘void arraysize(std::string&, int&, int&, std::string***, std::string*)’:
project_9_5.cpp:79:22: error: expected primary-expression before ‘[’ token
   { labs[i] = string [labsize[i]]; }
                      ^
project_9_5.cpp:81:46: error: expected primary-expression before ‘]’ token
  fillarrays(id, cstation, lab, labs, labsize[]);

My Code:

const string LAB1 = "5";
const string LAB2 = "6";
const string LAB3 = "4";
const string LAB4 = "3";

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]);
void fillarrays(string &id, int &lab, int &cstation, string **labs[], string labsize[]);
void printarrays(string &id, int &cstation, int &lab, string **labs[], string labsize[]);
void removearrays(string **labs[]);

int main() {
    string **labs[TOTAL_LABS];
    string labsize[TOTAL_LABS] = { LAB1,LAB2,LAB3,LAB4 };

    arraysize(id, cstation, lab, labs, labsize);
    printarrays(id, cstation, lab, labs, labsize);
    removearrays(labs);
return 0;
}

void removearrays(string **labs[]) {

    delete [] labs;
}

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]) {  //TOnnings example

    //int labsize[TOTAL_LABS] = { LAB1,LAB2,LAB3,LAB4 };

    labs = new string **[TOTAL_LABS];

    for(int i=0; i< TOTAL_LABS; i++)
        { labs[i] = string [labsize[i]]; }

    fillarrays(id, cstation, lab, labs, labsize[]);
}


void fillarrays(string &id, int &lab, int &cstation, string **labs[], string labsize[]) {

    for(int row = 0; row<TOTAL_LABS;row ++) {
        for(int col=0; col<TOTAL_LABS; col++) {
            **labs[row] = "Empty";
        }
    }   
}

void printarrays(string &id, int &cstation, int &lab, string **labs[], string labsize[]) {

    cout << "\nLabs#" << "  " << "Computer Stations" << endl;;
    for(int rows=0 ;rows<TOTAL_LABS; rows++) {
        cout << " " << (rows+1) << ":    ";
        for(int cols=0; cols<TOTAL_LABS; cols++) {
            cout << (cols+1) << ": ";
            cout << labs[rows] << "     ";
        }   
    cout << endl;
    }
    //menu(id, cstation, lab, labs);
}
2
  • All these pointers annoy me and surely any other reader. Pointers in C++ are mostly unnecessary and try to avoid them! And remember that C++ passes everything non-reference by value, so labs = new string **[TOTAL_LABS]; will assign to the argument. Commented Dec 5, 2015 at 20:35
  • I agree, I would be using vectors or some other type if I could, The assignment was specifically on pointers and dynamic arrays however, so can't be avoided :/ Commented Dec 5, 2015 at 21:13

1 Answer 1

1

Declaration of function:

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]);

You call this function:

arraysize(id, cstation, lab, labs, labsize);

cstation and lab not on their position .printarrays and fillarrays the same problem

I don't know what you want to do on this line

{ labs[i] = string [labsize[i]]; }

But you can not use type name like array

On this line, [] with labsize not neccessary.

fillarrays(id, cstation, lab, labs, labsize[]);

And removearrays need to changes:

void removearrays(string **labs[]) {
for (int i = 0; i < TOTAL_LABS; i++)
       delete [] labs[i];
delete [] labs;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This totally makes sense to me. THANK YOU. It would explain why I was getting Seg faults when I was messing with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.