i want to return an array of struct pointers.
i want the array of struct pointers became accessible in the main function. but it always breaks. i have tried using smart pointers but it seems like they don't do well with arrays or something. can someone tell me what step should i take to fix this problem?
sorry if the variable is written in Indonesian.
#include <iostream>
struct dataPasien {
int id;
int umur;
std::string nama;
std::string penyakit;
};
void daftarMenu() {
std::cout << "1. " << std::endl;
std::cout << "2. " << std::endl;
std::cout << "3. " << std::endl;
std::cout << "4. " << std::endl;
}
dataPasien* input() {
dataPasien* arr = new dataPasien[100];
int jumlah;
std::cout << "berapa pasien? ";
std::cin >> jumlah;
for(int i = 0; i < jumlah; i++) {
arr[i].id = i + 1;
std::cin.ignore();
std::cout << "nama? ";
getline(std::cin, arr[i].nama);
std::cout << "umur? ";
std::cin >> arr[i].umur;
std::cin.ignore();
std::cout << "penyakit? ";
getline(std::cin, arr[i].penyakit);
};
return arr;
}
int main() {
int menu = 0;
std::cout << "apa ?";
std::cin >> menu;
switch(menu) {
case 1: {
dataPasien* arr = input();
}break;
case 2:
std::cout << "it breaks";
case 3:
break;
case 4:
break;
default:
break;
}
std::cout << "it just breaks" ;
dataPasien* arr = arr;
for(int i = 0; i < 2;i++) {
std::cout << "id : " << arr[i].id << std::endl;
std::cout << "nama : " << arr[i].nama << std::endl;
std::cout << "umur : " << arr[i].umur << std::endl;
std::cout << "penyakit : " << arr[i].penyakit << std::endl;
}
return 0;
}
i have tried smart pointers but it seems like they don't do well with arrays. i've tried many things but it always ended with memory leaks
Rewrite your code to return a std::vector like this. (I see I should not have changed the getlines if you want to have a name contain white spaces)