简体   繁体   中英

C++ struct pointers

my code is not working. I have an error of -fpermissive (" invalid conversion from 'int' to 'persona*' [-fpermessive] "). Can you help me? This is my first real program, sorry for the errors and the bad english.

    #include <iostream>

using namespace std;

struct persona
{
    char nome[20];
    unsigned int eta;
    unsigned int altezza;
    unsigned int peso;
};

int totale = 0;
struct persona prs[100];

void leggere_dati(persona* prs)
{
    cout << "Persona numero: " << (totale + 1) << endl;
    cout << "Nome: " << prs->nome << endl;
    cout << "Eta': " << prs->eta << endl;
    cout << "Altezza: " << prs->altezza << endl;
    cout << "Peso: " << prs->peso << endl;
    cout << "-----------------------------------------------\n";
}

void inserire_dati(persona* prs)
{
    cout << "Nome? ";
    cin >> prs -> nome;
    cout << "\nEta'? ";
    cin >> prs -> eta;
    cout << "\nAltezza? ";
    cin >> prs -> altezza;
    cout << "\nPeso? ";
    cin >> prs -> peso;
}

int main()
{
     int risposte = 0;
    char risp = 0;

        do{
        inserire_dati(totale);
        cout << "Inserire un'altra persona? (S/N)" << endl;
        cin >> risp;
        if (risp == 'S')
    {
        risposte += 1;
        totale++;
        continue;
    }
    } while (risp == 's' || risp == 'S');

    if (risp == 'n' || risp == 'N')
    {
        for (totale = 0; totale <=  risposte; totale++)
            leggere_dati(totale);
    }
}

You are calling:

 inserire_dati(totale);

Defined as:

void inserire_dati(persona* prs);

While totale is:

int totale = 0;

That is the obvious error. But the background problem is that you don't have an object of the persona structure to read the data in. As I understand your code, that line should be:

inserire_dati(&prs[totale]);

You are accepting a pointer to the persona structure in that function, which is correct, since you are going to modify the structure's contents. totale holds the last position occupied (I think, but you should increment totale unconditionally). In order to get a pointer to the structure you use &, preceded with the object, which is prs[totale]. Since the name of the vector is a pointer to its beginning, prs + totale is also acceptable. In that case, you would be using pointer arithmetic, which is less readable.

Also, I don't understand the last part of main() .

Finally, if you are really using C++ , there is no real reason to use char[] instead of string .

The complete main() becomes:

int main()
{
    char risp = 0;

    do {
        inserire_dati(&prs[totale]);
        ++totale;
        cout << "Inserire un'altra persona? (S/N)" << endl;
        cin >> risp;
    } while (risp == 's' || risp == 'S');

    for(int i = 0; i < totale; ++i) {
         leggere_dati(&prs[totale]);
    }
}

But, well, getting further, I see you are using totale as a global variable. I'd wrap totale and prs in a whole structure.

struct personas {
    static const int Max = 100;
    struct prs[Max];
    int totale;
};

And a few other changes which you can find in IDEOne .

Hope this helps.

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