Compiler accuses typing error when there is no typing error

Asked

Viewed 72 times

1

I have the struct

struct no
{
int numero[2]; 
std::string nome;
float valorDoNome;
struct no *esq, *dir;
};
struct no *raiz, *aux1, *aux2,*favoritos;

The function

    {
    raiz->valorDoNome=0;
    int posicao=1;
    for(unsigned i=0; i<raiz->nome.length(); ++i)
    {

    if(raiz->nome.at(i)=='A' || (raiz->nome.at(i)='a'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(1/posicao);
    }
    if(raiz->nome.at(i)=='B' || (raiz->nome.at(i)='b'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(2/posicao);
    }
    if(raiz->nome.at(i)=='C' || (raiz->nome.at(i)='c'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(3/posicao);
    }
    if(raiz->nome.at(i)=='D' || (raiz->nome.at(i)='d'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(4/posicao);
    }
    if(raiz->nome.at(i)=='E' || (raiz->nome.at(i)='e'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(5/posicao);
    }
    if(raiz->nome.at(i)=='F' || (raiz->nome.at(i)='f'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(6/posicao);
    }
    if(raiz->nome.at(i)=='G' || (raiz->nome.at(i)='g'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(7/posicao);
    }
    if(raiz->nome.at(i)=='H' || (raiz->nome.at(i)='h'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(8/posicao);
    }
    if(raiz->nome.at(i)=='I' || (raiz->nome.at(i)='i'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(9/posicao);
    }
    if(raiz->nome.at(i)=='J' || (raiz->nome.at(i)='j'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(10/posicao);
    }
    if(raiz->nome.at(i)=='K' || (raiz->nome.at(i)='k'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(11/posicao);
    }
        if(raiz->nome.at(i)=='L' || (raiz->nome.at(i)='l'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(12/posicao);
    }
    if(raiz->nome.at(i)=='M' || (raiz->nome.at(i)='m'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(13/posicao);
    }
        if(raiz->nome.at(i)=='N' || (raiz->nome.at(i)='n'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(14/posicao);
    }
    if(raiz->nome.at(i)=='O' || (raiz->nome.at(i)='o'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(15/posicao);
    }
    if(raiz->nome.at(i)=='P' || (raiz->nome.at(i)='p'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(16/posicao);
    }
    if(raiz->nome.at(i)=='Q' || (raiz->nome.at(i)='q'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(17/posicao);
    }
    if(raiz->nome.at(i)=='R' || (raiz->nome.at(i)='r'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(18/posicao);
    }
    if(raiz->nome.at(i)=='S' || (raiz->nome.at(i)='s'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(19/posicao);
    }
    if(raiz->nome.at(i)=='T' || (raiz->nome.at(i)='t'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(20/posicao);
    }
    if(raiz->nome.at(i)=='U' || (raiz->nome.at(i)='u'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(21/posicao);
    }
    if(raiz->nome.at(i)=='V' || (raiz->nome.at(i)='v'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(22/posicao);
    }
    if(raiz->nome.at(i)=='W' || (raiz->nome.at(i)='w'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(23/posicao);
    }
    if(raiz->nome.at(i)=='X' || (raiz->nome.at(i)='x'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(24/posicao);
    }
        if(raiz->nome.at(i)=='Y' || (raiz->nome.at(i)='y'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(25/posicao);
    }
    if(raiz->nome.at(i)=='Z' || (raiz->nome.at(i)='z'))
    {
     raiz->valorDoNome=raiz->valorDoNome+(26/posicao);
    }
    posicao=posicao*100;
}
}

And call the function

calculaValorDoNome(raiz);

But when I call, it’s wrong:

[Error] could not convert 'raiz' from 'no*' to 'std::string {aka std::basic_string<char>}'

Even the function asking for a no* and I providing a no*.

Any idea why?

  • I was able to compile without problems, check if you are using the gcc or the g++ to compile

  • Obsere the (raiz->nome.at(i)='a') and assimilated. Note that you used = instead of ==. I think this must be wrong.

1 answer

3


First of all, look at this:

if(raiz->nome.at(i)=='A' || (raiz->nome.at(i)='a'))

In particular that:

raiz->nome.at(i)='a'

Note that you used = instead of ==. That must not be what you want.

And then we have this function:

calculaValorDoNome(raiz);

However, you did not put the statement of this function in your question. Only its body. It is possible that you have declared it wrong. But all right, follow the correct way to declare it:

void calculaValorDoNome(struct no *raiz);

Codes full of Ctrl+C Ctrl+V are horrible. Let’s try to simplify it and eliminate this pile of Ctrl+C Ctrl+V. Plus, identing it properly helps others understand it (and by ideating the code properly in your questions, you’ll tend to get more and better answers faster). Therefore, your code can be simplified until it becomes this:

struct no {
    int numero[2]; 
    std::string nome;
    float valorDoNome;
    struct no *esq, *dir;
};
struct no *raiz, *aux1, *aux2, *favoritos;

void calculaValorDoNome(struct no *raiz) {
    raiz->valorDoNome = 0;
    int posicao = 1;
    for (unsigned i = 0; i < raiz->nome.length(); ++i) {
        for (int c = 0; c < 26; c++) {
            if (raiz->nome.at(i) == ('A' + c) || raiz->nome.at(i) == ('a' + c)) {
                raiz->valorDoNome += ((c + 1) / posicao);
            }
        }
        posicao *= 100;
    }
}

However, I think that this is still not the algorithm you want, because from the moment that posicao reach 100, whatever the value of c, the expression ((c + 1) / posicao) will result in zero. Note that your original algorithm also had this same problem, since in the original algorithm (x/posicao) will result in 0 for any value of x between 1 and 26 when posicao has the value 100 or greater. Thus, as a result, we have that only the value of the first character ends up making some difference in the calculated value for the name, and I doubt that was its intention.

Unfortunately, since I don’t know what your idea is for an algorithm that gives a name a value, I can’t fix it without you telling me what it is you wanted him to actually do.

  • the idea was for example z=26 za=26,1 so I used floar instead of int

  • @user2296455 Yes, but as posicao is int and the value 1-26 before the operator / is also int, it will make a whole division. One possibility is to make a cast for float before carrying out the division.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.