-3

I'm writing a student management program in C++ for a college project. 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. And I have to do that using functions only. I can't assign the values inside of 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(). I tried to use pointers, but I have an issue using them with a structure.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
jak_es
  • 19
  • 3
  • *the functions are not passing the values inside the main* I don't understand this. – 273K Jun 16 '21 at 04:41
  • I mean when I call the function inside the main function then I'm trying lets say to print a student name or mark nothing happen – jak_es Jun 16 '21 at 04:50
  • `struct student stu[N];` is [not valid C++](https://stackoverflow.com/questions/1887097) when the value of `N` is not known until runtime. Use `new[]` or better `std::vector` instead. – Remy Lebeau Jun 16 '21 at 05:07
  • Where do you print something? No useful cout at all. – 273K Jun 16 '21 at 05:08
  • Both `names()` and `marks()` are declared as returning `student`, but they do not have any such `return` statement. They should be declared as returning `void` instead. – Remy Lebeau Jun 16 '21 at 05:10

1 Answers1

0

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