Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将值保存到数组中_C++ - Fatal编程技术网

C++ 将值保存到数组中

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

我在将输入流(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 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