I can not enter a char type vector in a linked list

-1
#include <iostream>
#include <stdlib.h>
using namespace std;

struct nodo{
    int nro;        // en este caso es un numero entero
    string nombre;
    char cedula[9];
    string apellido;
    struct nodo *sgte;
};

typedef struct nodo *Tlista;

void insertarInicio(Tlista &lista, int valor, string dato, char _cedula[9], string _apellido)
{
    Tlista q;
    q = new(struct nodo);
    q->nro = valor;
    q->nombre = dato;
    q->cedula[9] = _cedula[9];
    q->apellido = _apellido;
    q->sgte = lista;
    lista  = q;
}

void insertarElemento(Tlista &lista, int valor, int pos, string dato, char _cedula[9], string _apellido)
{
    Tlista q;
    q = new(struct nodo);
    q->nro = valor;

    if(pos==1)
    {
        q->sgte = lista;
        lista = q;
    }

    q = new(struct nodo);
    q->nombre = dato;

    if(pos==2)
    {
        q->sgte = lista;
        lista = q;
    }

    q = new(struct nodo);
    q->cedula[9] = _cedula[9];

    if(pos==3)
    {
        q->sgte = lista;
        lista = q;
    }

    q = new(struct nodo);
    q->apellido = _apellido;

    if(pos==4)
    {
        q->sgte = lista;
        lista = q;
    }
}

void reportarLista(Tlista lista)
{   

    while(lista != NULL)
    {
        cout <<' '<< "Numero: " << lista->nro << endl;
        cout <<' '<< "Nombre: " << lista->nombre << endl;
        cout <<' '<< "Cedula: " << lista->cedula << endl;
        cout <<' '<< "Apellido: " << lista->apellido << endl;
        lista = lista->sgte;
    }

}

void menu1()
{
    cout<<" 1. INSERTAR AL INICIO               "<<endl;
    cout<<" 2. REPORTAR LISTA                   "<<endl;
    cout<<" 3. LIMPIAR PANTALLA                   "<<endl;
    cout<<"\n INGRESE OPCION: ";
}


/*                        Funcion Principal
---------------------------------------------------------------------*/

int main()
{
    Tlista lista = NULL;
    int op;     // opcion del menu
    int _dato;  // elemenento a ingresar
    string _nom;  // elemenento a ingresar
    char _cedula2[9];
    string apellido;

    do
    {
        menu1();  
        cin>> op;

        switch(op)
        {
        case 1:

            cout<< "\n NUMERO A INSERTAR: "; cin>> _dato;
            cout<< "\n NOMBRE A INSERTAR: "; cin>> _nom;
            cout<< "\n CEDULA A INSERTAR: "; cin>> _cedula2;
            cout<< "\n APELLIDO A INSERTAR: "; cin>> apellido;
            insertarInicio(lista, _dato, _nom, _cedula2, apellido);
            break;

        case 2:

            cout << "\n\n MOSTRANDO LISTA\n\n";
            reportarLista(lista);
            break;

        case 3:
            system("cls");
            break;

        }

    }while(op!=8);
    return 0;
}
    
asked by Max 16.12.2018 в 02:17
source

3 answers

0

Look, I do not think I understand your question very well, but this

 void insertarInicio(Tlista &lista, int valor, string dato, char _cedula[9], string 
_apellido)
{
Tlista q;
q = new(struct nodo);
q->nro = valor;
q->nombre = dato;
q->cedula[9] = _cedula[9];
q->apellido = _apellido;
q->sgte = lista;
lista  = q;
}

It should be like that if you want to take the cedula arrangement ... remember that when you pass an arrangement what you are really going through is the reference to the first element and any change you make in any of the two changes the other

void insertarInicio(Tlista &lista, int valor, string dato, char _cedula[9], string 
_apellido)
{
Tlista q;
q = new(struct nodo);
q->nro = valor;
q->nombre = dato;
q->cedula = _cedula;
q->apellido = _apellido;
q->sgte = lista;
lista  = q;
}

now you will write down to _edula ... If this is your problem, please let me know ... Otherwise disuclpame me for misunderstanding the question, explain to me xq I am wrong and edit the question.Salu2;)

    
answered by 16.12.2018 в 03:57
0

The problem is that in C / C ++ you can not copy a vector with the operator "=", that is, your problem is on the line:

q->cedula[9] = _cedula[9];

You would solve this problem in the following way:

strcpy(q->cedula, _cedula);

But the variable cedula should be with a size of ten characters, 9 of cedula and one for the null character (the null character serves so that it does not print garbage characters).

    
answered by 16.12.2018 в 11:29
0

For future questions, please correctly explain the problem. We are not fortune tellers, so if you pose the problem in such a way that it is easy to understand your question, you will receive more answers and solutions.

This is important because, as you will see below, your program is a factory of both code and conceptual errors and in cases like this, it is possible that the person who answers you will forget about the problem that prevents you from working.

So you know, if you want quality answers, ask yourself a quality question.

In C ++ fixed-size arrays ( char array[10] ) can not be copied with the assignment operator because they do not have a valid overhead.

Since you are already using std::string in your program I suggest you replace the type of cedula so that it is also a std::string :

struct nodo{
    int nro;
    string nombre;
    string cedula; // <<---
    string apellido;
    struct nodo *sgte;
};

void insertarInicio(Tlista &lista, int valor, string dato, string _cedula, string _apellido)
//                                                         ~~~~~~
{
    Tlista q;
    q = new(struct nodo);
    q->nro = valor;
    q->nombre = dato;
    q->cedula = _cedula; // <<---
    q->apellido = _apellido;
    q->sgte = lista;
    lista  = q;
}

But here the thing does not end. C ++ is not C . C ++ is an object-oriented language, so it is not necessary to use struct everywhere:

struct nodo{
    int nro;
    string nombre;
    string cedula;
    string apellido;
    struct nodo *sgte;
//  ~~~~~~ Esto sobra
};

typedef struct nodo *Tlista; // <<--- Y esto también

void insertarInicio(nodo *& lista, ...) // No hace falta usar struct

In addition, the new operator can be used more intuitively:

nodo* q = new nodo;

And, to finish, note that your program is not able to exit the loop using option 3:

    case 3:
        system("cls");
        break;

That break serves to exit switch , but you would need a second break to exit the do-while loop. How can you not nest the loops:

    case 3:
        system("cls");
        break;
        break; // Este break no se ejecutaría nunca

The option is to correctly configure the condition of the do-while loop:

do
{
  // ..
} while(op!=3); // 3, no 8!!!!

And, by the way, although you still do not use it, the function insertarElemento is not going to work as expected ... this function is able to insert up to 4 elements in a single stroke (counts the number of times that% appears new in that function).

    
answered by 16.12.2018 в 12:24