并行数组父子对C ++

时间:2015-12-03 20:35:32

标签: c++ arrays string sorting parallel-arrays

我正在制作一个程序,按字母顺序对儿童进行排序并列出他们的父母。它应该允许一个人输入父母姓名,孩子的数量,然后输入孩子的名字。输出应该如下所示:

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

目前,我正在尝试设置数据输入。我尝试使用字符串作为父名和子名,并使用数组作为子项数;但是,代码无法正常运行。这是我到目前为止所做的:

#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];
                }

            }
        }   
    }   
}

我还没有启动排序功能,因为数据输入还没有起作用。我认为我的for循环有一些错误,因为它会重复令人作呕。

0 个答案:

没有答案