0

I'm working on a program that sorts children alphabetically and lists their parents. It should allow one to enter a parent name, the number of children, and then the childrens's names. The output is supposed to look like this:

This program prints parent-child pairs.
Enter parents and children below, use 'quit' to stop.
Parent: Kronos
How many children does Kronos have? 4
Children of Kronos: Hera Zeus Poseidon Hades
Parent: Hera
How many children does Hera have? 2
Children of Hera: Ares Heph
Parent: Zeus
How many children does Zeus have? 1
Children of Zeus: Athena
Parent: quit

Child    Parent
-----    ------
Ares     Hera
Athena   Zeus
Hades    Kronos
Heph     Hera
Hera     Kronos
Kronos
Poseidon Kronos
Zeus     Kronos

Currently, I am trying to get the data input set up. I tried using strings for the parent and children name and an array for the number of children; however, the code doesn't run properly. Here's what I have so far:

#include <iostream>
using namespace std;

int main()
{
    string parent;
    int childnum;
    string childname;

    cout << "This program prints parent-child pairs.
    cout << "Enter parents and  children below, use 'quit' to stop."
    cout << "Parent: ";
    for (int a = 0; a < 100; a++)
    {
        cin >> parent[a];
        while (parent != "quit")
        {
            for (int i = 0; i < 100; i++)
            {
                cout << "How many children does " << parent[a] << " have? ";
                cin >> childnum;
                cout << "Children of " << parent[a] << ": ";
                for (int b = 0; b < childnum; b++)
                {
                    cin >> childname[a];
                }

            }
        }   
    }   
}

I haven't started the sorting function yet, as the data input doesn't yet work. I think there is something erroneous with my for loop as it repeats ad nauseam.

0 Answers0