我正在开发一个按字母顺序对孩子进行排序并列出他们父母的程序。它应该允许一个人输入父母的名字,孩子的数量,然后是孩子的名字。输出应该如下所示:
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 循环有些错误,因为它重复令人作呕。