必要なのは、おそらく次のように配列を宣言することです。
char mess[10][80];
getlineから最大80文字を読み取っています。
現在の実装では、char*
割り当てられたバッファを指すように初期化されることのない10個の配列を作成します。
std::string
バッファサイズが自動的に処理されるため、はるかに安全な方法を使用することです。簡単な変更:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
std::string mess[10];
int i = 0;
for (; i < 10; i++)
{
cout << "Enter a string: ";
cin >> mess[i];
}
for (i = 0; i < 10; i++)
cout << mess[i] << endl; // you probably want to add endl here
system("PAUSE");
return EXIT_SUCCESS;
}
あなたが望むものをあなたに与えるべきです。
編集
どうしても必要な場合char *
(これは良い考えではありません)、探しているものは次のとおりです。
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
char* mess[10];
int i = 0;
for (; i < 10; i++)
{
cout << "Enter a string: ";
mess[i] = new char[80]; // allocate the memory
cin.getline(mess[i], 80);
}
for (i = 0; i < 10; i++)
{
cout << mess[i] << endl;
delete[] mess[i]; // deallocate the memory
}
// After deleting the memory, you should NOT access the element as they won't be pointing to valid memory
system("PAUSE");
return EXIT_SUCCESS;
}