1

recently I have started learning c++, so I'm trying to make a simple grade calculator using my base knowledge (I'm already good with Javascript so i know the basics of programming).

So in this case, I had to return an array of objects from a function call so i can use it later in the program, but I just couldn't find the right way to do so.

So basically i want to return subArr from the getInput function, but I couldn't do that due to my basic knowledge in the language. I tried googling but didn't find any simple solution.

Here is the code and hope it is simple:

//the Subject class:
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

//get Input function declaration
Subject getInput(int num){
    
    //array of objects of type "Subject"
    Subject subArr[num];
    
    //a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr[i]=Subject(name, grade, factor);
    };
    
    //trying to return the subArr at last
    return subArr;
};

//main function
int main(){
    //get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    //trying to receive the subArr from getInput call
    Subject subArr = getInput(numOfSubjects);
    
};
7
  • 8
    Use a std::vector<Subject> Commented Oct 15, 2020 at 14:19
  • 2
    Subject subArr[num]; is not valid c++, since num is a run-time value. Just use a std::vector instead. Commented Oct 15, 2020 at 14:19
  • @cigien but i need the num to initialize the array's size, also can u give me a code example of using std::vector? i'm a newbie, thanks Commented Oct 15, 2020 at 14:20
  • 1
    Thousands on the net. This one is from a solid documentation site you should bookmark. Commented Oct 15, 2020 at 14:24
  • 1
    @SalahEddineMakdour -- You should initialize your member variables in the Subject default constructor. Subject s; std::cout << s.grade; would produce undefined behavior, due to s.grade not being initialized. Commented Oct 15, 2020 at 14:30

1 Answer 1

2

Use std::vector:

#include <iostream>
#include <vector>

using namespace std;

// the Subject class
class Subject {
    public:
        string name;
        float grade;
        int factor;
        
        Subject(){};
        
        Subject(string x, float y, int z){
            name = x;
            grade = y;
            factor = z;
        }
};

// get Input function declaration
vector<Subject> getInput(int num){
    
    // array of objects of type "Subject"
    vector<Subject> subArr;
    
    // a for loop to assign the array's elements
    for(int i = 0; i < num; i++){
        string name;
        float grade;
        int factor;
        
        cout << "what is the name of subject " << i+1 <<"? "<<endl;
        cin >> name;
        
        cout << "what is the grade of subject " << i+1 << "? "<<endl;
        cin >> grade;
        
        cout << "what is the factor of subject " << i+1 << "? "<<endl;
        cin >> factor;
        
        subArr.push_back(Subject(name, grade, factor));
    };
    
    // trying to return the subArr at last
    return subArr;
};

// main function
int main(){
    // get the number of subjects
    int numOfSubjects;
    cout << "how many subjects are there? ";
    cin >> numOfSubjects;
    
    // trying to receive the subArr from getInput call
    vector<Subject> subArr = getInput(numOfSubjects);
};
Sign up to request clarification or add additional context in comments.

Comments

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.