Im trying to create an array and each object in the array should have the name Model(i) where is is index, Im doing this so they will have the names indexed in descending order Model5, Model4 ... Im trying to do that using char[] but for some reason in my code the use of strcat inside of for loop make me get stuck on an infinite loop, the second point if someone could help is convert the index in a way that I could concatenate with the name and give to the constructor.
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class CARRO {
public:
CARRO() {};
CARRO(char *modelo, unsigned ano);
char* getModelo();
unsigned getAno();
private:
char modelo[100];
unsigned ano;
};
void swap(int *p, int *q);
int partition(int *v, int start, int end);
int randomizedPartition(int *v, int start, int end);
void qsHelper(int *v, int start, int end);
void quickSort(int *v, int len);
void printList(CARRO *carros, unsigned len);
int main(int argc, char const *argv[]) {
CARRO carros[5];
unsigned len = sizeof(carros)/sizeof(CARRO);
for (int i = 0; i < len; ++i) {
char modelo[] = "Modelo";
char id[] = "I";
strcat(modelo, id);
unsigned ano = 1000 * (i+1);
carros[i] = CARRO(modelo, ano);
cout << carros[i].getModelo() << endl;
}
//printList(carros, len);
return 0;
}
CARRO::CARRO(char *modelo, unsigned ano) {
strcpy(this->modelo, modelo);
this->ano = ano;
}
If I remove the line :
strcat(modelo, id);
The loop works fine. I just cant understand why strcat is somehow generating an infinite loop. the output is this: (with the line strcat)
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
ModeloI
^CModeloI