[英]Passing structure containing an array to function
I'm writing a student management program in C++ for a college project.我正在为大学项目编写 C++ 中的学生管理程序。 I have to assign student names by user input, assign 5 different marks that are randomly chosen for each student, then calculate the average of the marks as the first question.
我必须通过用户输入分配学生姓名,为每个学生随机选择 5 个不同的分数,然后计算分数的平均值作为第一个问题。 And I have to do that using functions only.
我必须只使用函数来做到这一点。 I can't assign the values inside of
main()
.我无法分配
main()
内部的值。
My code is looking like this so far:到目前为止,我的代码看起来像这样:
#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;
}
The functions are not passing the values inside of main()
.这些函数没有传递
main()
内部的值。 I tried to use pointers, but I have an issue using them with a structure.我尝试使用指针,但是在将它们与结构一起使用时遇到了问题。
I have tried to solve your problem statement using below mention code.我试图使用下面提到的代码来解决您的问题陈述。
Feel free to write if you need any further help.如果您需要任何进一步的帮助,请随时写信。
Hope so it will be helpful.希望这会有所帮助。
#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.