简体   繁体   中英

How do I return the values of the Student Function to Main so that I can I use them for the Display Function?

Program:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//Structure
struct Student{
    string name;
    int pMark;
    int eMark;
    double avg;
    string result;
};

//Functions
Student info(int arrSize, Student arrStudent[10]);
void display(int arrSize, Student arrStudents[10]);

//Main program
int main() {
    Student arrStudents[10];
    int arrSize;

    cout << "How many Students are there(max 10): ";
    cin >> arrSize;

    info(arrSize, arrStudents);
    display(arrSize, arrStudents);

    return 0;
}

//Student Function
Student info(int arrSize, Student arrStudent[10]){
    int counter = 0;
    while(counter < arrSize){
        cout << "\nStudent " << (counter + 1) << " info:";
        cout << "\nEnter the name: ";
        cin >> arrStudent[counter].name;
        cout << "Enter the participation mark: ";
        cin >> arrStudent[counter].pMark;
        cout << "Enter the exam mark: ";
        cin >> arrStudent[counter].eMark;

        arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;

        if (arrStudent[counter].avg >= 50) {
            arrStudent[counter].result = "Pass";
        }
        else {
            arrStudent[counter].result = "Fail";
        }
        counter++;
    }

    return arrStudent;//(Return Array)?
}

//Display Function
void display(int arrSize, Student arrStudents[10]) {
    cout << endl << "Name\t\t Average\t\t Result" << endl;

    for (int counter = 0; counter < arrSize; counter++) { 
        cout << arrStudents[counter].name << "\t\t"
             << fixed << setprecision(2) 
             << arrStudents[counter].avg << "\t\t\t"
             << arrStudents[counter].result << endl;
    }
}

I tried using the function as such, but I'm not sure if it's correct?

//Student Function
Student info(int arrSize, Student arrStudent[10]){
    int counter = 0;
    while (counter < arrSize) {
        cout << "\nStudent " << (counter + 1) << " info:";
        cout << "\nEnter the name: ";
        cin >> arrStudent[counter].name;
        cout << "Enter the participation mark: ";
        cin >> arrStudent[counter].pMark;
        cout << "Enter the exam mark: ";
        cin >> arrStudent[counter].eMark;

        arrStudent[counter].avg = (arrStudent[counter].pMark + arrStudent[counter].eMark) / 2.00;

        if (arrStudent[counter].avg >= 50) {
            arrStudent[counter].result = "Pass";
        }
        else {
            arrStudent[counter].result = "Fail";
        }
        counter++;
    }
    return arrStudent[arrSize];
}

I'm new to coding(In University) so we still need to learn about vectors, pointers and references. That's why I haven't tried any other methods. I would highly appreciate the solutions if it is possible to solve it by avoiding those methods.

You are passing values of array by value , you should pass them by referance , thus you should use pointer ,

Example function:

Student info(int arrSize, int *arrStudent)

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