C++で動的配列を作成し、ユーザがexit
と入力するまで名前を入力する必要があります。連続した文字列入力用のC++動的配列
さらに多くの名前を尋ねて、動的文字列配列に記録してから、ユーザーが望むだけ多くの名前をランダムに選択する必要があります。
私は乱数部分を理解することができるはずですが、連続入力は私に問題を与えています。長さ変数の値の変更を継続する方法がわかりません。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int length;
string* x;
x = new string[length];
string newName;
for (int i = 0; i < length; i++)
{
cout << "Enter name: (or type exit to continue) " << flush;
cin >> newName;
while (newName != "exit")
{
newName = x[i];
}
}
cout << x[1] << x[2];
int qq;
cin >> qq;
return 0;
}
ご協力いただければ幸いです。
'length'には値がありませんが、あなたは"未定義の長さ "の配列を作ることができると思います - 配列のサイズを動的に変更しようとしていません。 forループ内のwhileループはナンセンスです。 – John3136
'std :: vector'を考えてみましょう。 –
ちょっと磨いてみると、 'const int length = 3;'と 'x [i] = newname'と' cout << x [0] .... ' – macroland