C++中字符串和字符数组的问题

Issues with strings and char arrays in C++

本文关键字:数组 问题 字符 字符串 C++      更新时间:2023-10-16

我正在编写一个注册表生成器,作为更大程序的一部分。我对C++很陌生,但擅长其他编程语言,比如PHP。

我将首先提供有问题的函数的代码:

void generacionAleatoria() {
    string r_marca, r_nom, r_apellido; 
    char r_patente[6];
    int num_rand; 
    registroAuto r_auto;

    string nombres[8] = {
        "Juan", "Pedro", "Roberto", "Miguel", "Guillermo", "Emilio", "Roque", "Gustavo"
    } ; 
    string apellidos[8] = {
        "Messi", "Maradona", "Gardel", "Heredia", "Pimpinela", "Nadal", "Mascherano", "Troilo"
    };
    string marcas[12] = {
        "Volvo", "Renault", "Audi", "Ford", "Fiat", "Chevrolet", "Nissan", "Volkswagen", "Mercedes Benz", "Rolls Royce", "Delorean", "Aston Martin"
    };
    char letras_patentes[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char numeros_patentes[] = "0123456789"; 
    for (int i = 0; i < cantidad_autos; i++) {
        r_marca = marcas[rand() % (sizeof(marcas)/sizeof(marcas[0]) - 1)];
        r_nom = nombres[rand() % (sizeof(nombres)/sizeof(nombres[0]) - 1)];
        r_apellido = apellidos[rand() % (sizeof(apellidos)/sizeof(apellidos[0]) - 1)];
        for(int m = 0; m < 3; ++m) {
            r_patente[m] = letras_patentes[rand() % (sizeof(letras_patentes) - 1)];
        }   
        for(int n = 3; n < 6; n++) {
            r_patente[n] = numeros_patentes[rand() % (sizeof(numeros_patentes) - 1)];
        }
        strcpy(r_auto.patente,r_patente);
        strcpy(r_auto.marca,r_marca.c_str());   
        strcpy(r_auto.apellido,r_apellido.c_str());
        strcpy(r_auto.nom,r_nom.c_str());
        fwrite(&r_auto,sizeof(registroAuto),1,archivo);
        if (ver_variables_testeo) { 
            //cout << (i+1) << ") " << r_auto.patente<<endl;
            cout << (i+1) << ") " << r_auto.marca << " - " << r_auto.patente << " - " << r_auto.nom << " " << r_auto.apellido << endl; //Para testear
        }
    }
}

这创建了100个以下类型的结构:

struct registroAuto {
    char marca[15];
    char patente[6];
    char nom[25];
    char apellido[25];
}; 

如果你想知道的话,这是优步司机及其汽车的登记处:品牌、车牌、姓名和姓氏。这不是真正的注册表,而是大学作业。

问题是,当我打印出我的新结构的内容时,车牌和名称将在一起,如:

100)菲亚特-KWQ293Maria-Maria Gardel

你可以通过连字符的位置看到,车牌现在是"KWQ293Maria",尽管它是一个由6个字符组成的数组!

cout命令提醒:

cout << (i+1) << ") " << r_auto.marca << " - " << r_auto.patente << " - " << r_auto.nom << " " << r_auto.apellido << endl;

我做了一些测试,但我不知道该怎么处理结果。

1:注释掉名称的strcopy修复了问题

    strcpy(r_auto.patente,r_patente);
    strcpy(r_auto.marca,r_marca.c_str());   
    strcpy(r_auto.apellido,r_apellido.c_str());
    //strcpy(r_auto.nom,r_nom.c_str());

正如你所看到的,这是我原始代码中4条语句中的最后一条,所以我不知道为什么它会影响r_auto.patent.

你能帮帮我吗?我猜我在课堂上错过了字符数组处理的一个关键概念:-(

当使用字符数组作为字符串时,它们需要由空字符''终止。因此,当你构建你的车牌时,你需要使数组7字符长。

struct registroAuto {
    char marca[15];
    char patente[7]; // 6 for numbers, 1 for terminator ''
    char nom[25];
    char apellido[25];
};

与您的工作变量相同:

char r_patente[7];

在创建数字时,您需要手动添加null终止符:

    for(int m = 0; m < 3; ++m) {
        r_patente[m] = letras_patentes[rand() % (sizeof(letras_patentes) - 1)];
    }
    for(int n = 3; n < 6; n++) {
        r_patente[n] = numeros_patentes[rand() % (sizeof(numeros_patentes) - 1)];
    }
    r_patente[6] = ''; // add the null terminator