C ++ - Problems with Char variable input

1

I am trying to record a series of data in a struct in c ++. However, I have the problem that the field "activepolicy" (what should have only V and F) does not take the values I want: Saves those from the subsequent field. I think it's a problem related to how to capture Char values. But I was looking for cplusplus.com and I could not find anything that made me understand.

This is the struct:

struct poliza{
    int nroPoliza;
    char dniAsegurado[11];
    char nombreAsegurado[50];
    char apellidoAsegurado[50];
    char cuotaAlDia[1];
    char patenteAuto[10];
    char polizaActiva[1];
    int cantIncidentes;
};

With this method I charge it

bool cargarNuevaPoliza(void){
    FILE *f;
    poliza p;
    if(f=fopen(ARCHIVOASEGURADOS, "a"))
    {
        cout << "Ingrese Numero de Poliza:" << endl;
        cin >> p.nroPoliza;
        cout << "Ingrese DNI:" << endl;
        cin >> p.dniAsegurado;
        cout << "Ingrese Nombre:" << endl;
        cin >> p.nombreAsegurado;
        cout << "Ingrese Apellido:" << endl;
        cin >> p.apellidoAsegurado;
        cout << "Posee la cuota al dia (V/F):" << endl;
        cin >> p.cuotaAlDia;
        cout << "Ingrese patente del auto:" << endl;
        cin >> p.patenteAuto;
        cout << "Poliza activa (V/F):" << endl;
        cin >> p.polizaActiva;
        cout << "Ingrese cantidad de Incidentes:" << endl;
        cin >> p.cantIncidentes;
        fwrite(&p, sizeof(poliza),1, f);
        fclose(f);
        return true;
    }
    return false;
}

With this I read:

void levantarAsegurados()
{
    FILE*F;
    int i = 0;
    poliza V [1000];
    F = fopen(ARCHIVOASEGURADOS,"rb");
    fseek(F,0,SEEK_SET);
    fread(&V[i],sizeof(poliza),1,F);
    while (!feof(F))
    {
        cout << V[i].nroPoliza << endl;
        cout << V[i].dniAsegurado << endl;
        cout << V[i].nombreAsegurado << endl;
        cout << V[i].apellidoAsegurado << endl;
        cout << V[i].cuotaAlDia << endl;
        cout << V[i].patenteAuto << endl;
        cout << V[i].polizaActiva << endl;
        cout << V[i].cantIncidentes << endl;
        i++;
        fread(&V[i],sizeof(poliza),1,F);
    }
    fclose(F);
}

The result I get is the following:

enter image description here

    
asked by jqc 31.08.2017 в 04:58
source

2 answers

1

To put this:

char polizaActiva[1];

Better leave it like this:

char polizaActiva;

More simple, clear and easy to understand. Think that text strings need at least two characters (one to store one character and another to end the string).

More than anything because then you do:

cin >> p.polizaActiva;

And supposedly, cin ends the text strings ... How is this supposed to end? I already tell you ... treading memory that does not belong to the array. However, if you leave the variable as a simple char , cin will not treat the variable as a string and will not try to end it.

Because of course, then you do:

cout << V[i].polizaActiva << endl;

And cout , as it is with an array of characters starts to take values until it meets a null character ... that will not be where you expect ... the string finisher that has set cin is no longer there because, presumably, has been trodden by subsequent operations.

If this last one does not fit you, let's see a possible example of use:

Initial memory (nothing initialized):

| polActiva |  cantIncidentes   | otras variables ... |
|           |    |    |    |    |    [basura]         |

operation:

cin >> p.polizaActiva; // supongamos que se introduce 'V'

memory status:

| polActiva |  cantIncidentes   | otras variables ... |
|     V     | 
cin >> p.cantIncidentes;
| | | | [basura] |

operation:

| polActiva |  cantIncidentes   | otras variables ... |
|     V     | 01 | 0A | 00 | 00 |    [basura]         |

state of memory (hypothetical example because it also depends on whether the machine is big-endian or little-endian ... among other things:

cout << p.polizaActiva

If you now do:

struct poliza{
    int nroPoliza;
    char dniAsegurado[11];
    char nombreAsegurado[50];
    char apellidoAsegurado[50];
    char cuotaAlDia;
    char patenteAuto[10];
    char polizaActiva;
    int cantIncidentes;
};

The function will begin to traverse bytes and will print them until a '\ 0' is found ... and as you see the chain terminator that you had initially lost.

The structure should look like this:

struct poliza{
    int nroPoliza;
    std::string dniAsegurado;
    std::string nombreAsegurado;
    std::string apellidoAsegurado;
    char cuotaAlDia;
    std::string patenteAuto;
    char polizaActiva;
    int cantIncidentes;
};

Although taking into account that you are in C ++, it would be best to leave it like this:

char polizaActiva[1];
    
answered by 31.08.2017 / 08:41
source
0

Replaces char QuotaAlDia [1]; for char QuotaAlDia; and             char polizaActiva [1]; for char polizaActiva; . That is, it eliminates the declaration of the array. Do not give you an error in F = fopen (ARCHIVOINSURED, "rb"); ?, it should be ARCHIVOASEGURADOS not in quotation marks

    
answered by 31.08.2017 в 08:55