0

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);
    }
}
2
  • The variable totale, what type is it? Why do attempt to pass it as an argument to the function inserire_dati? Are you trying to pass e.g. &prs[totale]? Commented Jul 7, 2017 at 9:49
  • You should tell readers where that error occurs, i.e. by including the full error text, which should include the line number. Commented Jul 7, 2017 at 11:23

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

worth noting is to use appropriate containers like std::vector and std::string.
@TheTechel Sometimes you just don't need to have dynamic memory, like vectors. (Therefor, I agree that std::string would be a great improvement in this code). If you only need 100 persons, that array is just ok.
@Shirkam But if you often have fewer than 100, allocating more is wasteful. How many real programs need only fixed numbers of objects? And even if they have a max, how many waste space by allocating a fixed length? The pattern seems like one taught by tutorials to beginners, as if they need another thing with little use in the real world. IME the useful cases for fixed-length arrays are for buffers to compose strings for C, interface with hardware, etc. And those cases are fairly rare. For arbitrary objects this seems hardly useful at all. Learning about std::vector would be much more useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.