/** * Test driver for printing out the family tree of one file. * @param argc The number of command line arguments * @param argv[] The command line arguments, * the second of which should be the file name * @return EXIT_SUCCESS if the program has succeeded, EXIT_FAILURE otherwise. */ int main( int argc, char* argv[] ) { char fileData[1000]; // Character array for file less than 1000 characters // Initialize the character array with a command-line file if( initDataFromFile( argv[1], fileData ) == EXIT_FAILURE ) return EXIT_FAILURE; { int tabCount = 0; // Stores the tab amounts for following generations int currentIndex = 0; // Stores the current index of the person // Print the first parent fprintf( stdout, "%c", fileData[ currentIndex ] ); fprintf( stdout, "-" ); // Print the second parent currentIndex++; fprintf( stdout, "%c", fileData[ currentIndex ] ); fprintf( stdout, "\n" ); // Set the index and tab count to prepare for the parent's children currentIndex = 2; tabCount++; // Create the family tree if( createFamilyTree( fileData, tabCount, currentIndex ) == EXIT_FAILURE ) { fprintf( stderr, "Unable to create family tree.\n" ); return EXIT_FAILURE; } } // Successfully created the family tree! return EXIT_SUCCESS; }
bool FamilyTree::verifyOption() { while(!(_option == "1" || _option == "2" || _option == "3")){ cout <<"Try again please you type an invalid option: "; cin >> _option; } if(_option == "1"){ createFamilyTree(); return true; } else if(_option == "2"){ showFamilyTree(); return true; } else{ cout << "See, back soon" << endl; return false; } _option=""; }
/** * Creates the family tree. * @param fileData[] The character data to parse the family tree from. * @param tabCount The current tab count for formatting children. * @param currentIndex The current person to print out. * @return EXIT_FAILURE if there is an issue in the fork(), * EXIT_SUCCESS otherwise. */ int createFamilyTree( char fileData[], int tabCount, int currentIndex ) { // Set the number of children currently int numChild = ( fileData[ currentIndex ] - '0' ); currentIndex++; // Loop through all of the children int iCount = 0; for( iCount = 0; iCount < numChild; iCount++ ) { // Check if the current person's spouse exists int spouseLocation = findSpouse( fileData, currentIndex ); // Spouse exists if( spouseLocation > -1 ) { // Create a new process to handle more children pid_t pid = fork(); // CHILD FORK OPERATIONS if( pid == 0 ) { // Print out the required tabs for this generation for( iCount = 0; iCount < tabCount; iCount++ ) fprintf( stdout, "\t" ); // Print out the couple fprintf( stdout, "%c", fileData[ currentIndex ] ); fprintf( stdout, "-" ); fprintf( stdout, "%c", fileData[ spouseLocation ] ); fprintf( stdout, "\n" ); // Create the couple's family tree createFamilyTree( fileData, tabCount + 1, spouseLocation + 1 ); return EXIT_SUCCESS; } // PARENT FORK OPERATIONS else if( pid > 0 ) { // Wait until child operations are done waitpid( pid, NULL, 0 ); } else { // Something went wrong fprintf( stderr, "Fork Error: ID less than zero.\n"); return EXIT_FAILURE; } } // Spouse does not exist else { // Print out the required tabs for this generation int printCount = 0; for( printCount = 0; printCount < tabCount; printCount++ ) fprintf( stdout, "\t" ); // Print out the loner's name fprintf( stdout, "%c\n", fileData[ currentIndex ] ); } // Check the next person currentIndex++; } // Family tree created! return EXIT_SUCCESS; }