简体   繁体   中英

C++ How to return array of objects from a function

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);
    
};

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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM