记录不同方式的控制台输入。(动态输入,以特殊字符结束。)
如下两种输入方式:
#define MAX_SAMPLE_NUM 100 // 最大样本个数
double **SAMPLE; // 样本集
int SAMPLE_NUM = 0; // 样本个数
int NUMBER = 2; // 维数
第一种方式:getline
void input()
{
int i, j;
double temp;
cout << "请输入样本:\n";
SAMPLE = new double*[MAX_SAMPLE_NUM];
for (i = 0; i<MAX_SAMPLE_NUM; i++)
{
SAMPLE[i] = new double[NUMBER];
}
SAMPLE_NUM = 0;
i = 0;
string str;
while (true)
{
j = 0;
getline(cin, str, ' ');
if (str == ";")
break;
else
{
temp = atof(str.c_str());
cout << temp << " ";
SAMPLE[i][j] = temp;
}
for (j = 1; j < NUMBER - 1; j++)
{
getline(cin, str, ' ');
temp = atof(str.c_str());
cout << temp << " ";
SAMPLE[i][j] = temp;
}
getline(cin, str, '\n');
temp = atof(str.c_str());
cout <<

本文详细介绍了C++中控制台输入的两种常见方式:getline和cin。getline函数允许以特定字符为结束标志进行动态输入,而cin则在遇到回车或空格时结束输入,并继续处理下一个输入项。这两种方法在不同的输入场景中有各自的应用。

2950

被折叠的 条评论
为什么被折叠?



