0

I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string> 
using namespace std;

class subject{

   public:
      int numOfStudents;
      string subjectId;
      int * marksArray;
      int * indexArray;
      char * gradeArray;
      int index ; // index for inserting data

      subject(int students , string subjectName){

      numOfStudents = students;
      subjectId = subjectName;
      this->index =0 ;
      //creating and pointing to arrays
      int  A[numOfStudents];      marksArray = A;
      int  B[numOfStudents];      indexArray = B;
      char C[numOfStudents];      gradeArray = C;

  }

 void insert(int studentId , int mark , char grade){
 indexArray[index] = studentId;
 marksArray[index] = mark;
 gradeArray[index] = grade; 

 this->index = this->index +1;    
}

int getNumberOfEntries(){
    return index ;
}

 void printEntries(){
        cout<< indexArray[0] << "  O" << marksArray[0] << "  " << gradeArray[0] << endl; 
        cout<< indexArray[1] << "  OOO" << marksArray[1] << "  " << gradeArray[1] << endl; 
        cout<< indexArray[2] << "  OO" << marksArray[2] << "  " << gradeArray[2] << endl;  
      }
  };

 int main(int argc, char const *argv[]){ 
  subject S(10,"Mathematics");
  cout<<S.subjectId<<endl;
  cout<<S.numOfStudents<<endl;
  S.insert(35,34,'A');
  S.insert(33,34,'B');
  S.insert(54,34,'C');
  S.insert(21,34,'D');
  S.insert(14,34,'F');
  S.printEntries();
  return 0;
 }

output is :

Mathematics 10 35 O34 A 0 OOO0

7
  • Can you please post the output that you do get? Commented Nov 20, 2018 at 4:16
  • 3
    1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const. Commented Nov 20, 2018 at 4:16
  • @DMarczak done ! Commented Nov 20, 2018 at 4:19
  • @paddy do you have any suggestions ? Commented Nov 20, 2018 at 4:20
  • You mean, apart from what I just provided?? Commented Nov 20, 2018 at 4:21

1 Answer 1

2

As pointed out by @paddy in the comments to your question, your issue is within the constructor.

//creating and pointing to arrays
int  A[numOfStudents];      marksArray = A;
int  B[numOfStudents];      indexArray = B;
char C[numOfStudents];      gradeArray = C;

What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"

Replacing the pointers with std::vectors and inserting you elements there will be must easier.

class subject
{
public: // It's considered bad practive to have public data members in a class
  std::vector<int> marksVector;
  std::vector<int> indexVector;
  std::vector<char> gradeVector;
  // ...

  void insert(int studentId , int mark , char grade){
     indexVector.push_back(studentId);
     marksVector.push_back(mark);
     gradeVector.push_back(grade);

     // No longer need to worry about index for next insert
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
@Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.

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.