简体   繁体   中英

Pointer to structure array shows only the first character of a char field

I have the following code:

#include <iostream>

using namespace std;

struct Materie {   //grades 
    float romana;
    float matematica;
    float fizica;

};

struct Elev {  // students structure, name, prename and grade obtained;
    char nume[30];
    char prenume[30];
    Materie nota;
};

void absolvent(Elev* elevi, int &n) { // fuction to remove studens with grade < 5
    for (int i = 0; i < n; i++) {
        if (elevi[i].nota.fizica < 5.0 || elevi[i].nota.matematica < 5.0 || elevi[i].nota.fizica < 5.0) {
            for (int j = i; j < n; j++) {
                *elevi[j].nume = *elevi[j + 1].nume;
                *elevi[j].prenume = *elevi[j + 1].nume;
                elevi[j].nota.romana = elevi[j + 1].nota.romana;
                elevi[j].nota.matematica = elevi[j + 1].nota.matematica;
                elevi[j].nota.fizica = elevi[j + 1].nota.fizica;
            }
            n--;
            i--;
        }
    }
}


int main() {
    int n;
    do {
        cout << "Introduceti numarul de elevi: ";
        cin >> n;
        if (n < 0 || n > 30) {
            cout << "0 < n < 30";
        } 
    } while (n < 0 || n > 30);
    Elev* elevi = new Elev[n];
    for (int i = 0; i < n; i++) {
        cout << "Introduceti numele elevului " << i + 1 << " : ";
        cin >> elevi[i].nume;
        cout << "Introduceti prenumele elevului " << i + 1 << " : ";
        cin >> elevi[i].prenume;
        cout << "Introduceti nota obtinuta la limba romana: ";
        cin >> elevi[i].nota.romana;
        cout << "Introduceti nota obtinuta la matematica: ";
        cin >> elevi[i].nota.matematica;
        cout << "Introduceti nota obtinuta la fizica: ";
        cin >> elevi[i].nota.fizica;
    }
    cout << elevi[0].nume << endl;
    cout << *elevi[0].nume << endl;

    return 0;
}

I want to implement a function that removes the Elev (student), from the array, if one of the grades he obtained is less than 5. I want to do it by using array, not vectors.

While doing the exercise, the following questions araised:

1). Why, in the absolvent fuction, the IDE expects me to pass * to elevi[i].nume (i get an error if i input only elevi[i].nume: "expression must be a modifiable lvalue"), but, for the elevi[i].nota.romana it's the opposite (i get an error trying to put elevi[i].nota.romana: "operand of ' " must pe a pointer")?

2). Why does the elevi[0].nume, returns "Alex" for example if i input Alex in console, but for *elevi[0].nume returns only A?

The reason for *elevi[j].nume only showing the first character is that it is the same as elevi[j].nume[0] , which is more obvious.

You can't assign to arrays, but you can assign to structs and classes that contain arrays, and the solution is much simpler than you were assuming.

for (int j = i; j < n-1; j++) {
    elevi[j] = elevi[j+1];
}

will do exactly what you want.
(Note that you need to lower the end of the loop, or you will go outside the valid indices.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM