c++arraysfunction

different arrays in one function


I am beginner in C++, and I have to write a function that asks the user to enter the values of the first element of five different arrays. For example array for student name, array for student id, etc. The question here is what would be the parameter of this function.

This is my attempt. I declared the arrays to be global. I do not know where is the mistake.

const int SIZE=1000;
int studN[SIZE]; 
int id[SIZE]; 
string courseName[SIZE];
string courseSec[SIZE]; 
int regNom[SIZE];
    
void insertNew()
{ 
int index=0;
index++;
cout<<"Please enter the student name:  ";
cin>>studN[index];
cout<<"Please enter the student ID:  ";
cin>>id[index];
cout<<"Please enter the course name:  ";
cin>>courseName[index];
cout<<"Please enter the course section:  ";
cin>>courseSec[index];
cout<<"Please enter the registration number:  ";
cin>>regNom[index];
cout<<" Information stored"<<endl;
}

Solution

  • If I have understood you correctly you want to write a function that will set values of elements of the arrays with a given index. The function can look the following way

    bool Insert( int studN[], int id[], string courseName[], string courseSec[], int regNom, size_t size, size_t pos )
    {
        if ( size <= pos ) return false;
    
        cout << "Please enter the student name:  ";
        cin >> studN[pos];
        // other stuff
        // ,,,
    
        return true;
    }
    

    But in any case it would be better to define one array of a structure or class that will contain corresponding fields.