私は子供をアルファベット順に並べ替え、両親をリストするプログラムに取り組んでいます。親の名前、子供の数、そして子供の名前を入力できるようにする必要があります。出力は次のようになります。
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 ループには何か問題があると思います。