0

so i am making a program that takes student data as objects and prints it, i am trying to store the data as an array so each student is a element in the array, i am having issues with printing the data though as i cannot pass the array into the print function, can anyone help? when i try and compile i get "studentarray: arrays of references are illegal". sorry if this is a stupid question i am new to coding, thankyou!


  • 3
    `main();` is a bug. The c++ standard says you are not permitted to call main() ever. Related: [https://stackoverflow.com/questions/27250120/how-do-i-call-main-in-my-main-cpp-file-from-a-seperate-cpp-file](https://stackoverflow.com/questions/27250120/how-do-i-call-main-in-my-main-cpp-file-from-a-seperate-cpp-file) – drescherjm Jan 25 '22 at 13:08
  • 4
    If you want the number of elements to be decided based on user input, don't use an array. Use the standard library class, specifically `std::vector`, which manages a dynamically allocated array (so your code can change its size as needed). Plenty of introductory examples around about how to use `std::vector`. – Peter Jan 25 '22 at 13:10
  • `student enterstudent();` is a declaration. – drescherjm Jan 25 '22 at 13:11
  • @drescherjm So is `student enterstudent;`. Just a different type of declaration. – Peter Jan 25 '22 at 13:12
  • My point for mentioning that is I believe the OP wanted to call a function not declare one. – drescherjm Jan 25 '22 at 13:14
  • 1
    Inside `void enterstudent()` you have `student studentarray[10];` you should not be creating an array here. Your `student` class should not manage an array of students. It should represent a single student. In `int main()` you should probably have a `std::vector students;` and your `printstudent()` function should print 1 student. – drescherjm Jan 25 '22 at 13:26
  • Calling `main` recursively is possible but that doesn't mean you should do it. You will soon have an stack overflow. – digito_evo Jan 25 '22 at 14:23

2 Answers2

0

You wrote student printstudent(student & studentarray); which looks more like a deceleration than a function call.

I'm guessing you wanted to write printstudent(studentarray)?

Your error is caused by declaring printstudent(student& studentarray[10]) , which declares the function argument to be an array of 10 student& i.e. references to students, which the c++ standard forbids:

There shall be no references to references, no arrays of references, and no pointers to references.

Some more details on why can be found here: Why are arrays of references illegal?

0

After a bit of refactoring and fixing bugs, here it is:

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


class Student
{
public:
    std::string name;
    std::string email;
    std::string number;
    int grade1, grade2, grade3, grade4, totalGrade;
    float percentage1, percentage2, percentage3, percentage4, totalPercentage;

public:
    static void addStudent( std::vector<Student>& students )
    {
        std::cout << "\nEnter the number of students: ";
        std::size_t count { };
        std::cin >> count;

        std::cout << '\n';

        for ( std::size_t idx { }; idx < count; ++idx )
        {
            std::cout << "Enter the details of student #" << idx + 1 << ":\n";
            students.resize( students.size( ) + 1 );
            students.back( ).inputStudentDetails( );
            std::cout << '\n';
        }

        std::cout << '\n';
    }


    void inputStudentDetails( )
    {
        std::cout << "Enter Student name: ";
        std::cin >> name;
        std::cout << "Enter student number: ";
        std::cin >> number;
        std::cout << "Enter student email: ";
        std::cin >> email;
        std::cout << "Enter grade for test1: ";
        std::cin >> grade1;
        std::cout << "Enter grade for test2: ";
        std::cin >> grade2;
        std::cout << "Enter grade for test3: ";
        std::cin >> grade3;
        std::cout << "Enter grade for test4: ";
        std::cin >> grade4;

        percentage1 = static_cast<float>( grade1 );
        percentage2 = static_cast<float>( grade2 );
        percentage3 = static_cast<float>( grade3 );
        percentage4 = static_cast<float>( grade4 );
        totalPercentage = percentage1 + percentage2 + percentage3 + percentage4;
    }

    void printStudentDetails( ) const
    {
        std::cout << "Student Name: " << name << "\nStudent Number: " << number
                  << "\nStudent email: " << email << "\ngrade1: " << grade1
                  << "\ngrade2: " << grade2 << "\ngrade3: " << grade3
                  << "\ngrade4: " << grade4 << "\ntotal " << totalPercentage << " out of 400\n";
    }

    static void printAllStudentsDetails( const std::vector<Student>& students )
    {
        std::size_t idx { };

        for ( const Student& stud : students )
        {
            std::cout << "\nDetails of student #" << ++idx << ":\n";
            stud.printStudentDetails( );
            std::cout << std::setfill( '-' ) << std::setw( 28 ) << '\n';
        }
    }
};


int main( )
{
    std::vector<Student> students;

    while ( true )
    {
        std::cout << "\t\tMENU\n" << std::setfill( '_' ) << std::setw( 36 ) << "\n\n";
        std::cout << "1: Enter student details\n";
        std::cout << "2: Display all students' details\n";
        std::cout << "3: Exit the Application\n";
        char select { };
        std::cin >> select;

        switch ( select )
        {
            case '1' : Student::addStudent( students ); break;
            case '2' : Student::printAllStudentsDetails( students ); break;
            case '3' : return 0;
            default : std::cout << "Invalid Input!\n";
        }
    }
}
digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • 1
    thanks dude! this was exactly what i was trying to make but struggling to figure out! im going to go through it now and understand how it works! many thanks :) –  Jan 25 '22 at 18:19
  • @dookam1 Basically, there is a vector in `main` called `students` that can store multiple `Student` objects. You pass it to the functions `addStudent` and `printAllStudentsDetails` and they do the work. – digito_evo Jan 25 '22 at 19:22