I'm trying to create a simple family tree in C++ by utilizing pointers in order to input strings for names that are then saved step-by-step within the code.
For example, the program starts at an unknown spot, and the user is prompted to either enter a name for the current spot, move to the dad's spot, move to the mom's spot, or go back to the starting person (first person registered). This is what I've got so far:
#include <iostream>
#include <cstdlib>
using namespace std;
class person
{
public:
person* mom;
person* dad;
string name;
person();
~person();
person(string n);
};
person::person()
{
mom = nullptr;
dad = nullptr;
name = "Unknown";
}
person::~person()
{
//delete any dynamic memory if needed
}
person::person(string n)
{
name = n;
}
int main()
{
string inputName;
person current;
person *pointer; //I know I'll probably need to use a class pointer
//in some manner to keep track of the current pointer, but how?
//also, how to turn private variables in person into public
//while still being able to use them indirectly in main, if possible?
char choice;
do {
cout << "You are currently at: " << current.name << endl;
cout << "Your mom is: ";
if(current.mom == nullptr)
{
cout << "???" << endl;
}
else
{
cout << current.mom;
}
cout << "Your dad is: ";
if(current.dad == nullptr)
{
cout << "???" << endl;
}
else
{
cout << current.dad;
}
cout << "Give name, go to mom, go to dad, back to start, or quit?" << endl;
cin >> choice;
if (choice == 'g')
{
cout << "What name (single word)?" << endl;
cin >> inputName;
current.name = inputName;
}
else if (choice == 'm')
{
//go to mom spot
}
else if (choice == 'd')
{
//go to dad spot
}
else if (choice == 'b')
{
//go to first registered name
}
}
while (choice == 'g' || choice == 'm' || choice == 'd' || choice == 'b');
//else quit
return 0;
}
My code is quite sloppy at the moment, as I'm a complete beginner to C++ and only started about a week ago on my own. I know public variables are a really bad practice, but I don't really know how to use private variables within main. Any help with how to organize my pointers or how to make private variables work within main would be greatly appreciated.