This is my first question here. I hope I'm doing everything right.
So, the title speaks for itself. I am working on Windows10, Visual Studio. This basic code will later be addopted in a windowed application (c++ / cli).
I am not a programmer. I learned to program in C, 15 years ago under Linux and haven't used it since. I started picking up c++ coding recently.
Here is an example code I am struggling with:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
struct mystructure {
string id;
int ra_h;
int ra_m;
float ra_s;
int dec_deg;
int dec_min;
string constellation;
string object;
string mag;
string arc_size;
string name;
};
int load_csv( struct mystructure* _table) {
//I cut out the csv loader part to make the code shorter.
int i = 0;
while (i++ < 10) {
_table = (mystructure*)realloc(_table, (i + 1) * sizeof(mystructure)); //(i+1) --> +1 might be unnecessary, but it's not working
_table[i].id = "id";
_table[i].ra_h = i + 1;
_table[i].ra_m = i + 2;
_table[i].ra_s = i + 3.3;
_table[i].dec_deg = i + 4;
_table[i].dec_min = i + 5;
_table[i].constellation = "name of constellation";
_table[i].object = "name of object";
_table[i].mag = "mag value";
_table[i].arc_size = "text..";
_table[i].name = "text..";
}
return i; //return the number of lines on the csv file
}
int main() {
//struct mystructure table[15]; //not dynamic but working without realloc
struct mystructure table[1]; //use realloc in function loop
int num_of_lines;
num_of_lines = load_csv(table); //this function fills table struct
//printout for debug
for (int i = 1; i < num_of_lines; i++) {
std::cout << "Ciklus count: " << i << '\n';
std::cout << "Id: " << table[i].id << '\n';
std::cout << "Ra: " << table[i].ra_h << "h " << table[i].ra_m << "m " << table[i].ra_s << "s" << '\n';
std::cout << std::showpos << "Dec: " << table[i].dec_deg << "\370 ";
std::cout << std::noshowpos << table[i].dec_min << "' " << '\n';
std::cout << "Const: " << table[i].constellation << '\n';
std::cout << "Object: " << table[i].object << '\n';
std::cout << "Mag: " << table[i].mag << '\n';
std::cout << "Arc Size: " << table[i].arc_size << '\n';
std::cout << "Name: " << table[i].name << '\n';
std::cout << "-------------------" << '\n';
}
return 0;
}
I am filling up the table struct array with load_csv function. If I specify the size of the struct array the code work but I can not make it dynamic. I tried a number of ways nothing worked. This is just one example I put here.
I'd appreciate any comment and help to solve this issue.
Thank you Robert