C++ 将值保存到数组中
我在将输入流(cin)中的值保存到数组中时遇到了一些问题C++ 将值保存到数组中,c++,C++,我在将输入流(cin)中的值保存到数组中时遇到了一些问题 int count = 2; double *startValues = new double[count]; for(int i = 0; i < count; i++) { double tmpVal; cout << i + 1 << ". Startwert: "; cin >> tmpVal; startValues[i] = tmpVal; } int cou
int count = 2;
double *startValues = new double[count];
for(int i = 0; i < count; i++) {
double tmpVal;
cout << i + 1 << ". Startwert: "; cin >> tmpVal;
startValues[i] = tmpVal;
}
int count=2;
double*startValues=新的双精度[计数];
for(int i=0;i
在for循环之后,只有第一个值保存在数组startValues中,而不是第二个值。这里出了什么问题?错误检查。您需要验证是否确实从
cin
int count = 2;
double *startValues = new double[count];
int i = 0;
while (i < count) {
double tmpVal;
cout << i + 1 << ". Startwert: ";
if (cin >> tmpVal) {
startValues[i] = tmpVal;
++i;
} else {
cout << "\nIncorrect entry, try again\n";
}
}
int count=2;
double*startValues=新的双精度[计数];
int i=0;
而(我<计数){
双tmpVal;
库特(tmpVal){
起始值[i]=tmpVal;
++一,;
}否则{
cout在实现您的版本后,我发现了我的初始错误。没有插入双精度值(0.0),而是插入int(0)。感谢您的帮助;)您应该使用std::vector
。