簡體   English   中英

將包含數組的結構傳遞給 function

[英]Passing structure containing an array to function

我正在為大學項目編寫 C++ 中的學生管理程序。 我必須通過用戶輸入分配學生姓名,為每個學生隨機選擇 5 個不同的分數,然后計算分數的平均值作為第一個問題。 我必須只使用函數來做到這一點。 我無法分配main()內部的值。

到目前為止,我的代碼看起來像這樣:

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

using namespace std;

float random_marks(){
   float Q[7];
   float j{2};
   for(int i{0};  i < 7; i++){
       Q[i] = j;
       j += 0.5;
   }
   float x = Q[rand() % 7];
   return x;
}

struct student{
    string imie;
    float oceny[5];
    float srednia;
};

student names(student stu[], int N){
     float srednia{0};
     for(int i =0; i < N; i++){
         cout << "Prosze podac imie studenta Nr" << (i + 1) <<endl;
         cin >> stu[i].imie;
     }
}

student marks(student stu[], int N){
     float srednia{0};
     for(int i = 0; i < N; i++){
         for(int j; j < 5; j++){
           float x = losuj_oceny();
           stu[i].oceny[j] = x;
         }
     }
}

int main(){
   
   srand((unsigned) time(0)); 
   int N{0};
   cout << "prosze poac liczbe studentow ";
   cin >>N;
   struct student stu[N];
   names(stu, N);
   marks(stu, N);

   return 0;
}

這些函數沒有傳遞main()內部的值。 我嘗試使用指針,但是在將它們與結構一起使用時遇到了問題。

我試圖使用下面提到的代碼來解決您的問題陳述。

如果您需要任何進一步的幫助,請隨時寫信。

希望這會有所幫助。

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <numeric>

// Total subject count 
const int total_subjects = 5;

// Operator overloaded to print elements of vector
template <typename T>
std::ostream & operator << (std::ostream & os, const std::vector<T> & elements)
{
    int subject_count = 0;
    for(auto each_element : elements)
    {
        os<< "Mark of subject " << (subject_count+1)  << " : "<< each_element<< "\n";
        ++subject_count;
    }
    return os;
}

// Our structure to store data of students
struct student{
    std::string name;
    std::vector<float> marks;
    float average ;
};

// Returns random float number on every call
float random_marks(){
   float Q[7];
   float j{2};
   for(int i{0};  i < 7; i++){
       Q[i] = j;
       j += 0.5;
   }
   float x = Q[rand() % 7];
   return x;
}

// Calculates average from current marks of student
void calculate_average(struct student &p_data_container)
{
    p_data_container.average = ((std::accumulate(p_data_container.marks.cbegin(),p_data_container.marks.cend(),0))/total_subjects);
}

// Seeks name of student
void get_student_name(struct student &p_data_container)
{
    std::string current_name;
    std::getline(std::cin >> std::ws, current_name);
    p_data_container.name = current_name;
}

// Seeks marks of student
void get_student_marks(struct student &p_data_container)
{
    for(int subject_number = 0 ; subject_number < total_subjects; ++subject_number)
    {
       p_data_container.marks.push_back(random_marks());
    }
}

// Used to display data of container
void display_student_data(struct student &p_data_container)
{
    std::cout << "Student Name : " << p_data_container.name << "\n";
    std::cout << "Student Marks : \n" << p_data_container.marks << "\n";
    std::cout << "Average of Marks : " << p_data_container.average << "\n";
}

int main(){

    srand((unsigned) time(0));

    int total_students = 0;
    std::cout << "Please enter student count : ";
    std::cin >>total_students;

    struct student* database = new struct student[total_students];

    for(int student_number = 0; student_number < total_students ; ++student_number)
    {
        std::cout << "Please enter the name of the student No. " << (student_number + 1)<<std::endl;
        get_student_name(database[student_number]);
        get_student_marks(database[student_number]);
        calculate_average(database[student_number]);
        display_student_data(database[student_number]);
    }

    delete[] database;
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM