[Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

This is a discussion on [Error] invalid conversion from 'char' to 'const char*' [-fpermissive] within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone. I don't know what to do, I'm new at this so if anyone can help me, it would ...

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]

    Hello, everyone.

    I don't know what to do, I'm new at this so if anyone can help me, it would be very appreciated.

    I got this error in the 227 line of my code.

    Thank you.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    using namespace std;
    
    
    char resp;
    
    
    typedef struct nodito
    {
        float n;
        struct nodito *pt;
    }nod;
    
    
    class PILITA
    {
        private:
            nod *tope;
        public:
            PILITA(){tope=NULL;}
            void push();
            void pop();
            void elimina_pila();
            void imprime_pila();
            void busca_dato();
            void primero();
            void ultimo();
            ~PILITA();
    };
    
    
    void PILITA::push()
    {
        do
        {
            float nuevo_dato;
            nod *nvo;
            nvo=(nod*)malloc(sizeof(nod));
            cout<<"\n Dame el elemento que deseas agregar a la pila: ";
            cin>>nuevo_dato;
            nvo->n=nuevo_dato;
            if(tope==NULL)
            {
                nvo->pt=NULL;
                tope=nvo;
            }
            else
            {
                nvo->pt=tope;
                tope=nvo;
            }
            cout<<"\n Deseas agregar otro elemento a la pila? [S/N]: ";
            cin>>resp;    
        }while(resp=='s'||resp=='S');
    }
    
    
    void PILITA::pop()
    {
        do
        {
            if(tope==NULL)
            {
                cout<<"\n La pila esta vacia \n";
                system("pause");
                return;
            }
            nod *aux;
            aux=tope;
            tope=aux->pt;
            cout<<"\n Sale: "<<aux->n;
            free(aux);
            cout<<"\n Deseas sacar otro elemento de la pila? [S/N]: ";
            cin>>resp;
        }while(resp=='s'||resp=='S');
    }
    
    
    void PILITA::imprime_pila()
    {
        if(tope==NULL)
        {
            cout<<"\n La pila esta vacia \n";
            system("pause");
            return;
        }
        nod *aux;
        aux=tope;
        while(aux!=NULL)
        {
            cout<<"\n "<<aux->n;
            aux=aux->pt;
        }
        cout<<"\n";
        system("pause");    
    }
    
    
    void PILITA::elimina_pila()
    {
        cout<<"\n Estas seguro que deseas eliminar la pila? [S/N]: ";
        cin>>resp;
        if(resp=='s'||resp=='S')
        {
            while(tope!=NULL)
            {
                if(tope==NULL)
                {
                    cout<<"\n La pila esta vacia \n";
                    system("pause");
                    return;
                }
                nod *aux;
                aux=tope;
                tope=aux->pt;
                cout<<"\n Sale: "<<aux->n;
                free(aux);    
            }
            cout<<"\n";
            system("pause");    
        }
    }
    
    
    void PILITA::busca_dato()
    {
        if(tope==NULL)
        {
            cout<<"\n La pila esta vacia \n";
            system("pause");
            return;
        }
        int buscador;
        nod *aux;
        aux=tope;
        cout<<"\n Que elemento deseas buscar?: ";
        cin>>buscador;
        while(buscador!=aux->n)
        {
            aux=aux->pt;
            if(aux==NULL)
            {
                cout<<"\n No se encontro el elemento \n";
                system("pause");
                return;
            }
        }    
        cout<<"\n Dato encontrado: "<<aux->n<<"\n";
        system("pause");
    }
    
    
    void PILITA::primero()
    {
        if(tope==NULL)
        {
            cout<<"\n La pila esta vacia \n";
            system("pause");
            return;
        }
        cout<<"\n El primer dato es: "<<tope->n<<"\n";
        system("pause");    
    }
    
    
    void PILITA::ultimo()
    {
        if(tope==NULL)
        {
            cout<<"\n La pila esta vacia \n";
            system("pause");
            return;
        }
        nod *aux;
        aux=tope;
        while(aux->pt!=NULL)
        {
            aux=aux->pt;
        }
        cout<<"\n El ultimo dato es: "<<aux->n<<"\n";
        system("pause");
    }
    
    
    PILITA::~PILITA()
    {
        while(tope!=NULL)
        {
            if(tope==NULL)
            {
                cout<<"\n La pila esta vacia \n";
                system("pause");
                return;
            }
            nod *aux;
            aux=tope;
            tope=aux->pt;
            cout<<"\n Sale: "<<aux->n;
            free(aux);    
        }    
    }
    
    
    void menu()
    {
        char opc='i';
        int opc1;
        PILITA p1;
        do
        {
            cout<<"\n 1.Insertar dato.";
            cout<<"\n 2.Sacar dato.";
            cout<<"\n 3.Mostrar pila.";
            cout<<"\n 4.Eliminar pila.";
            cout<<"\n 5.Buscar dato.";
            cout<<"\n 6.Mostrar el primer elemento.";
            cout<<"\n 7.Mostrar el ultimo elemento.";
            cout<<"\n 8.Salir.";
            cout<<"\n\n Selecciona una opcion: ";
            cin>>opc;
            if(isdigit(opc))
            {
                opc1=atoi(opc); //this is were I have the error.
                switch(opc1)
                {
                    case 1:
                        p1.push();
                        break;
                    case 2:
                        p1.pop();
                        break;
                    case 3:
                        p1.imprime_pila();
                        break;
                    case 4:
                        p1.elimina_pila();
                        break;
                    case 5:
                        p1.busca_dato();
                        break;
                    case 6:
                        p1.primero();
                        break;
                    case 7:
                        p1.ultimo();
                        break;
                    case 8:
                        cout<<"\n Gracias por utilizar el programa."<<"\n";
                        system("pause");
                        break;
                    default:
                        cout<<"\n Opcion incorrecta. Vuelva a intentarlo."<<"\n";
                        break;
                }        
            }
            else
            {
                cout<<"\n Error. Porfavor ingrese un numero. \n";
                system("pause");
            }
        }while(opc!=8);
    }
    
    
    main()
    {
        menu();
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    See the man of atoi. You need to pass a string, not a character! But if you do that, isdigit will probably cause an error, because it requires an int argument.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    atoi is for turning a string into an int.
    To turn a single character into an int, simply subtract '0'.
    Or just leave it as a character and make your cases '1' instead of 1, etc.
    Then you could get rid of the isdigit check, too, and handle the error condition in the default case of the switch.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    opc is of type int. atoi() accepts a const pointer to char, not an int. Hence the warning - there is no sensible conversion between an int and a pointer.


    Looking at your code, you're trying to read an int, but then cope with the possibility of a user entering data that can't be interpreted as an integral value. You're going about that all wrong.

    cin >> opc will read an int. If it can't do that (eg the stream contains the letter 'X') it will put the stream itself into an error state and not modify the variable opc. If you need your program to cope with such input, you need to check the stream state and recover if it is in an error state.

    Since opc is not modified if the user enters bad input, isdigit(opc) and atoi(opc) are not even a sensible part of techniques to cope.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    @grumpy: opc is a char. opc1 is the int.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    In that case, oogabooga

    1) The cause of the warning is the same. char is an integral type, and there is no sensible conversion between integral types and pointers (I'm using the word "sensible" in the sense of "not causing undefined behaviour if the resultant pointer is actually used").

    2) It just means the approach the OP is trying to use is even worse than I thought, but my answer is largely valid. If you can assume nine or less options, no need to use isdigit() or atoi(). Just change the switch cases - like you said - to '1', '2', etc rather than 1,2, etc. That won't work if ten or more options ever need to be supported though, as reading a single char is not enough - but the approach I suggested will work.

    3) Having variable names like opc and opc1 actually contributes to increased unreadability, which is why I got caught.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-09-2011, 12:41 PM
  2. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  3. invalid conversion from 'const char*' to 'char'
    By howzer in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2006, 11:58 AM
  4. invalid conversion from `const char*' to `char'
    By GameGenie in forum C++ Programming
    Replies: 6
    Last Post: 08-01-2005, 05:30 AM
  5. invalid conversion from 'char' to 'const char*'
    By LiKWiD in forum C++ Programming
    Replies: 10
    Last Post: 03-20-2005, 02:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21