初め。cin.getline(input,32);
スペースで区切られた入力を取得するために 使用しますが、機能しcin>>input
ません。
について。char*
_ char**
以下のコードは、各単語を配列に折りたたみます。
#include<windows.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
struct CommandArray
{
vector<char*> Arr;
USHORT Num;
};
CommandArray StrToArray(LPSTR Str)
{
CommandArray Out;
LPSTR TempStr;
Out.Num = 0;
TempStr = strtok (Str," ");
while(TempStr != NULL)
{
Out.Arr.push_back(TempStr);
Out.Num++;
TempStr = strtok(NULL," ");
}
return Out;
}
int main()
{
int ROWS=80; //80 characters wide
int COLUMNS=20;// 20 lines
int i;
char seperators[] = " ,\t\n";
char *token;
char* input_Dynamic1DCharPointerArray = new char[80];
char **output_Dynamic2DCharPointerArray = 0;
//memory allocated for elements of rows.
output_Dynamic2DCharPointerArray = new char *[ROWS] ;
//memory allocated for elements of each column.
for( i = 0 ; i < ROWS ; i++ ) output_Dynamic2DCharPointerArray[i] = new char[COLUMNS];
strcpy(input_Dynamic1DCharPointerArray,"apples 123 oranges 456 bananas 789 lemons 101112" );
//cout<<" \n";
//cin.getline(input_Dynamic1DCharPointerArray,32);
cout<<" \ninput = "<<input_Dynamic1DCharPointerArray<<" \n\n";
cout<<"Output = \n";
token = strtok( input_Dynamic1DCharPointerArray, seperators );
i=0;
while( token != NULL )
{
strcpy(output_Dynamic2DCharPointerArray[i],token);
cout<<output_Dynamic2DCharPointerArray[i]<<" \n";
token = strtok( NULL, seperators ); // C4996
i++;
}
cout<<" \n";
delete[] input_Dynamic1DCharPointerArray;
//free the allocated memory
for( i = 0 ; i < ROWS ; i++ )
delete [] output_Dynamic2DCharPointerArray[i] ;
delete [] output_Dynamic2DCharPointerArray ;
return 0;
}