简体   繁体   English

如何将学生 Function 的值返回到 Main,以便我可以将它们用于显示 Function?

[英]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?我尝试使用 function 这样,但我不确定它是否正确?

//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:示例 function:

Student info(int arrSize, int *arrStudent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能将数组值返回到main函数? - why can't i return array values to main function? 为什么我不能从主函数返回更大的值? - Why can't I return bigger values from main function? 如何使用函数进行计算,并使用空函数进行显示? - How do i use a function to calculate, and use a void function to display? 我可以在main()函数之外使用GetAsyncKeyState()吗? - Can I use GetAsyncKeyState() outside of the main() function? 我从我的字符串函数返回到主函数是什么? - What do I return to the main function from my string function? 如何获得图像的蒙版,以便可以在修复功能中使用它 - how to get a mask of an image so that i can use it in the inpainting function "<i>why i cant return 2 value in a user defined function and display it in the main functionn?<\/i>为什么我不能在用户定义的函数中返回 2 个值并将其显示在主函数中?<\/b> <i>can someone help me<\/i>有人能帮我吗<\/b>" - why i cant return 2 value in a user defined function and display it in the main functionn? can someone help me 如何在 function C++ 中返回多个值? - How do I return multiple values in a function C++? 如何从 C++ function 返回数百个值? - How do I return hundreds of values from a C++ function? 如何在不直接调用函数的情况下将值返回给主函数 - How do I return value to main function without directly calling the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM