I have a struct solar_system, and a struct planet. I want to create solar_system test_system, which should then contain planet planets, then assign a string to test_system.planets[1 through max_planets].name, and then view the name of each planet.
Heres the relevant code
The issue comes up when I try to access test_system.planets[n].nameCode:struct planet { char name[]; int solar_system_position; }; struct solar_system { char name[]; int max_planets; planet planets[]; int n; solar_system() { generate_random_name(name); max_planets=rand()%9+1; n=1; while(n<=max_planets) { generate_random_name(planets[n].name); planets[n].solar_system_position=n; n++; } } }; void generate_random_name(char* name) { int n = rand()%3+1; switch(n) { case 1: strcpy(name,"one");break; case 2: strcpy(name,"two");break; case 3: strcpy(name,"three");break; case 4: strcpy(name,"four");break; default: mvprintw(0,0,"INVALID NAME GENERATION");break; } }
For whatever reason instead of printing "one" or "two" etc it prints "^A" "^B" and so on. If I add a line within solar_system() to print planets[n].name as it is generated everything is fine; it prints "one" and "two" and so on as would be expected. The issue only seems to come up when I try to access the name from outside of solar_system()
I hope I explained the situation well enough, if more information is needed I'd be happy to provide it. I am still sort of a newbie so the code is probably not the prettiest but any help with this particular issue would be appreciated.



3Likes
LinkBack URL
About LinkBacks



CornedBee