价值更新?为什么? [英] values updating? why?
问题描述
#include<iostream>
using namespace std;
int main(){
int iCount=0;
int array[iCount], iInput,i;
cout<<"HOW MANY NUMBERS:" ;
cin >> iInput;
for(i=0; i<iinput;++i){>
cin>> array[iCount];
iCount++;
}
return 0;
}
取n个数字的简单代码。
可以解释为什么iInput正在更新它的值为6.
当我输入时,让我们在数组输入6个条目后说8。 iInput更新为6.
请帮助。我不明白:(((
simple code in taking n numbers.
can someone explain why iInput is updating its value to 6.
when I inpputed lets say 8 after 6 entries to the array. iInput updates itself to 6.
Please help. I dont understand :((((
推荐答案
嗯......它有点复杂。
Well...it's slightly complex.
#include<iostream>
using namespace std;
int main(){
int iCount=0;
int array[iCount], iInput,i;
cout<<"HOW MANY NUMBERS:" ;
cin >> iInput;
for(i=0; i<iinput;++i){>
cin>> array[iCount];
iCount++;
}
return 0;
}
查看数组
的定义 - 它是一个整数数组,但是你可以存储的整数数量是受控制的通过iCount - 当你创建数组时为零(因为上面的行表示如此)。
所以当你到达存储值的时候数组:
Look at your definition of array
- it's a array of integers, but the number of integers you can store in it is controlled by iCount - which at the time you create the array is zero (because the line above says so).
So when you get to the point of storing a value in the array:
cin>> array[iCount];
实际上没有任何地方可以存储它,所以它会覆盖堆栈上的下一个变量 - 恰好是iInput。
给数组一个大小,它应该开始工作。
there isn't really anywhere to store it, so it will overwrite the "next" variable on the stack - which happens to be iInput.
Give the array a size, and it should start to work.
这篇关于价值更新?为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!