1
votes
  1. To the best of my knowledge, when a function receives a const parameter, the function cannot change it. so, what supposed to happen when the function should change the parameter? (For instance the function contains "cin" commend to the const parameter). Would it be compilation error? or would it run but the parameter don't change in practice?
  2. I tried to do some tests in the code below. When I set from void read_student (Student students[], int size) to void read_student (const Student students[], int size), I receive the following error messages (these are only some of them). Does this happen because the combination of the 'const' parameter and the 'cin' commend? If it is, how am I supposed to understand that from these messages?

|19|error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const char [20]')|

|19|error: invalid initialization of non-const reference of type 'bool&' from an rvalue of type 'bool'|

|19|error: invalid conversion from 'const char*' to 'short int' [-fpermissive]|

|19|error: cannot bind rvalue '(short int)((int)(&(students + ((sizetype)(((unsigned int)i) * 24u)))->Student::name))' to 'short int&'|

|19|error: invalid conversion from 'const char*' to 'short unsigned int' [-fpermissive]|

|19|error: cannot bind rvalue '(short unsigned int)((int)(&(students + ((sizetype)(((unsigned int)i) * 24u)))->Student::name))' to 'short unsigned int&'|

#include <iostream>

using namespace std;

const int max_students=3;

struct Student
{
    char name [20];
    float avg;
};


void read_student (const  Student students[], int size) //const Student VS Student
{
    for (int i=0; i<size; i++)
    {
        cout << "enter name and avg for student #" << i+1 << endl;
        cin >> students[i].name >> students[i].avg;
    }
}


void print_student (const Student students[], int size)
{
    for (int i=0; i<size; i++)
        cout << "name: " << students[i].name << "\taverage: " << students[i].avg <<endl;
}




int main()
{
    Student students[max_students];
    read_student(students, max_students);
    cout << "ell students: \n";
    print_student(students, max_students);


    return 0;
}
3

3 Answers

2
votes

You get a compilation error because the operator>> doesn't apply to a constant variable as you can see in the doc. So the error happens because of the combination of setting the variable to a constant and using operator>>. Removing one or the other solves the issue

Here is a sample code you can try out to see for yourself.

void Foo(int const x) {
    std::cin >> x;    // doesn't compile because operator>> not defined for int const
}

void Foo2(int x) {
    std::cin >> x;    // Compiles properly
}

In your case the function constructor should be read_student(Student students[], int size) since students[] is modified in the function.

The error line error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream}' and 'const char [20]') indicates that the no operator>> overloading exists for this specific variable type e.g const char[].

I hope this answered your question.

1
votes
void print_student (Student *students, int size)
{
    for (int i=0; i<size; i++)
        cout << "name: " << students[i].name<<"\taverage<<students[i].avg<<endl;

}

and to call it:

print_student (&students,  size);
1
votes

Looks like homework... There you go:

void read_student(Student students[], int size) //const Student VS Student
{
    for (int i = 0; i<size; i++)
    {
        cout << "enter name and avg for student #" << i + 1 << endl;
        cin >> students[i].name >> students[i].avg;
    }
}

The hint is quite obvious, //const Student VS Student, now the thing is a constant is constant, meaning after creating it it's values cannot be changed. They are a great way to ensure that new containers have to be created upon interactions with them, instead of mingeling with the values inside a container.